簡體   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