How to move file/folder to an already existing folder [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working on something like recycle bin, that I need to move folder/files to already existing folder, I tried to use
Directory.Move
but it creates new directory and that's wrong for me, I have a specific directory to move to.
Can you help me?

It seems do don't actually want to move the folder, you want to move the contents of the folder. If you want to do that, you have to tell the computer to do that:
void MoveContentsOfDirectory(string source, string target)
{
foreach (var file in Directory.EnumerateFiles(source))
{
var dest = Path.Combine(target, Path.GetFileName(file));
File.Move(file, dest);
}
foreach (var dir in Directory.EnumerateDirectories(source))
{
var dest = Path.Combine(target, Path.GetFileName(dir));
Directory.Move(dir, dest);
}
// optional
Directory.Delete(source);
}

Related

File.Copy Access Denied [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem with copying from local disk to flash. The code worked before, but after I create anoteher foreach loop and I had to create new objects, the File.Copy functionality isn't working anymore.
In value File | System.IO.File there are values like: Error_Access_Denied | 5, Error_Invalid Parameter| 87, GetFileExInfoStandard | 0.
edit: locationUSB present file path on flash. (locationUSB == "D:\something.hex") and x._location == "C:\something_1.hex" .
foreach (object item in this.dataGridView2.Rows)
{
versionOnDisk = this.VersionInt(x._version);
versionOnFlash = this.VersionInt(((DataGridViewRow)item).Cells[2].Value.ToString());
if (versionOnFlash > versionOnDisk)
forbidCopying = true;
else
locationUSB = _logicalDrive + ((DataGridViewRow)item).Cells["Filename"].Value.ToString(); // <-- location value (because of the foreach)
if (!forbidCopying)
File.Copy(x._location, locationUSB, true); // <--
else if (AllowDelete.Checked)
File.Delete(locationUSB);
}
edit:
If I change the location into logical drive path, which value is "D:\" I get the DirectoryNotFoundException was unhandled: Could not find a part of the path 'D:\'.
Most probably after creating new files, you are not closing the FileStreams. Close the FileStreams of the newly created files using either myFile.Close(); or create new files inside using (var myFile = File.Create(myPath)) blocks.

Environment.CurrentDirectory in Resources [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to put this string in my resources:
Environment.CurrentDirectory + \\Server Files\\
But whenever I load the string, the Environment.CurrentDirectory part is shown as normal text instead of the current directory path :(.
Example:
Console.WriteLine(Resources.ServerFilesLocation); // Doesn't give me a path, but just plain text.
Any runtime environment value cannot be stored as a string in resources. What you should do is create a layer between. Something like:
static class ResourceManager
{
public static string ServerFilesLocation {
get {
return String.Format(Resources.ServerFilesDirectory, Environment.CurrentDirectory);
// ServerFilesDirectory = "{0}\\Server Files\\" or something similar
}
}
}
And use it like: Console.WriteLine(ResourceManager.ServerFilesLocation);

How to create a folder in c# windows forms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to create a folder automatically when a solution is installed in the computer i.e( Local Disk:D) using c# windows forms?
Solution#1:
On first run of the application (make an xml file to track the first execution) you may create folder.
Solution#2: (Good one)
You may check if that directory exists ,if not then create the directory
try
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(palettesPath))
{
Directory.CreateDirectory(palettesPath);
}
}
catch (Exception)
{
// Fail silently
}
Source:
check this link
How to Add Items to a Deployment Project
http://msdn.microsoft.com/en-us/library/z11b431t.aspx
Specifically:
How to: Add and Remove Folders in the File System Editor
http://msdn.microsoft.com/en-us/library/x56s4w8x.aspx
See the MSDN for more info, but the gist is:
You set the path like either of the ways below. See string literals for more info on this:
string newPath = #"c:\yourpath";
or
string newPath = "c:\\yourpath\\morepath";
To create the directory, you use this:
System.IO.Directory.CreateDirectory(newPath);
Hope this helps!

Open a file in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to read all the files in a folder. If the name is index.html, nothing happens. It's not even opening the file in the browser.
This is the code I use .
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
FileStream fileStream = new FileStream(path + thefile.Name, FileMode.Open, FileAccess.Read);
}
}
All that your code this is create a FileStream pointing to this file. So you could read the file and fetch its contents in memory. But you cannot expect it to open in any browser. You could use the Process.Start method to open the file using the default program that is associated with this file type:
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
Process.Start(thefile.Name);
}
}
Your code is putting the contents of the file into a FileStream so you can use it in your code. You'll need to do something with that FileStream next.
If you wanted the file opened using the default application (i.e. appearing in a browser) use this:
System.Diagnostics.Process.Start(thefile);

What is the most robust way to open file and read content [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I usually do this when I have to read the whole file always:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
Any better/faster way?
string content = System.IO.File.ReadAllText(#"your file path");
If you want to get lines:
string[] lines = System.IO.File.ReadAllLines(#"your file path");
if you want to only read lines until get to some line then use the IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(#"your file path")
{
if (line == ...)
{
break;
}
}

Categories