[英]How can I solve this? “A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll”
I've recently started using Visual C# because I thought it would be interesting to try something different than Unity for once. 我最近开始使用Visual C#,因为我认为尝试一次不同于Unity的东西会很有趣。 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. 现在,我已经弄清楚了目录扫描并过滤掉了所有我需要使用DirectoryInfo和FileInfo列表进行的扩展,这不会引起任何明显的问题。 What's killing me is the FileInfo.MoveTo method. 杀死我的是FileInfo.MoveTo方法。 I set a string variable, which you will see below, as a path. 我设置了一个字符串变量,您将在下面看到它作为路径。 Then placed that as the string argument for MoveTo. 然后将其作为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. 一切似乎都有意义,当我将“ target.Name”移动到根目录时,它可以工作,但是如果我将“ Directory.GetCurrentDirectory()”或“ target”以外的任何其他目录都不能工作。名称”,因为它本身就是。
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. 当您使用当前目录( Directory.GetCurrentDirectory();
)时,似乎您正在尝试覆盖exe和dll。
second you need to provide a fully qualified path as file name. 第二,您需要提供标准路径作为文件名。 A directory path maps to a folder. 目录路径映射到文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.