I've recently started using Visual C# because I thought it would be interesting to try something different than Unity for once. So I tried to make a program, that searches through set directories, grabs files based on the extensions I need, and then moves them into a folder.
Now I have figured out the directory scan and filtering out all, the extensions I need using DirectoryInfo and FileInfo lists, which is not causing any evident problems. What's killing me is the FileInfo.MoveTo method. I set a string variable, which you will see below, as a path. Then placed that as the string argument for MoveTo. Everything seems to make sense, and it works when I put "target.Name" as it moves the files to the root, but won't work if I put "Directory.GetCurrentDirectory()" Or any other directory other than "target.Name" because it's itself.
Apologies for the lengthy question, but this is really killing me.
static List<FileInfo> _targetFiles = new List<FileInfo>();
static string _targetPath = Directory.GetCurrentDirectory();
static void Main(string[] args)
{
CopyFiles(_targetFiles, _targetPath);
Console.WriteLine("Scan finished.");
Console.Read();
}
static void CopyFiles(List<FileInfo> files, string path)
{
foreach (FileInfo target in files)
{
try
{
target.MoveTo(_targetPath);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(target + " moved to " + _targetPath);
Console.ResetColor();
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("TRANSFER FAILED: " + target);
Console.ResetColor();
}
}
}
Output:
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
Console
TRANSFER FAILED: filename.ext
Not sure if I get the question properly. When you are using current directory (Directory.GetCurrentDirectory();
), then it seems that you are trying to over-write your exe and dlls.
second you need to provide a fully qualified path as file name. A directory path maps to a folder.