繁体   English   中英

我该如何解决? “ mscorlib.dll中发生了'System.IO.IOException'类型的第一次机会异常”

[英]How can I solve this? “A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll”

我最近开始使用Visual C#,因为我认为尝试一次不同于Unity的东西会很有趣。 因此,我尝试制作一个程序,该程序可以搜索设置的目录,根据所需的扩展名获取文件,然后将其移动到文件夹中。

现在,我已经弄清楚了目录扫描并过滤掉了所有我需要使用DirectoryInfo和FileInfo列表进行的扩展,这不会引起任何明显的问题。 杀死我的是FileInfo.MoveTo方法。 我设置了一个字符串变量,您将在下面看到它作为路径。 然后将其作为MoveTo的字符串参数。 一切似乎都有意义,当我将“ target.Name”移动到根目录时,它可以工作,但是如果我将“ Directory.GetCurrentDirectory()”或“ target”以外的任何其他目录都不能工作。名称”,因为它本身就是。

对于这个冗长的问题表示歉意,但这确实使我丧命。

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();
            }

        }
    }

输出:

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll

安慰

TRANSFER FAILED: filename.ext

不知道我是否正确地提出了问题。 当您使用当前目录( Directory.GetCurrentDirectory(); )时,似乎您正在尝试覆盖exe和dll。

第二,您需要提供标准路径作为文件名。 目录路径映射到文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM