簡體   English   中英

進行 while 循環以將所有 (.exe) 文件從一個目錄復制到另一個目錄

[英]Make a while loop to copy all (.exe) files from a directory to another directory

        while (true)
    {
        foreach (var file in Directory.GetFiles("C:\\Windows\\Fonts", "*.sys", SearchOption.AllDirectories))
            File.Copy(file, Path.Combine("F:\\Output", Path.GetFileName(file)), true);

        foreach (var file in Directory.GetFiles("C:\\Windows\\Fonts", "*.exe", SearchOption.AllDirectories))
            File.Copy(file, Path.Combine("F:\\Output", Path.GetFileName(file)), true);   
    }

那么我想做的是運行一個應用程序,該應用程序在字體中安裝 .exe 文件並在幾秒鍾后刪除它們。 我試圖抓住它們並將它們放在 F:\\ 中,當我通過手動將 .exe 文件放在 Fonts 文件夾中進行測試時,它會將它們放在 Output 文件夾中,但是當我使用應用程序執行此操作時,我的代碼顯示錯誤:

System.IO.IOException: '進程無法訪問文件 'C:\\Windows\\Fonts\\app.EXE',因為它正被另一個進程使用。

它只是試圖復制它。

撇開將 .exe 文件移動到您的 Font 文件夾很奇怪的事實......

錯誤消息准確地告訴您問題是什么。 您不能移動另一個進程正在使用的應用程序。 我處理遇到此錯誤的可能性的方法是使用try and catch IOException處理程序。 它看起來像這樣......

while (....)
{
    try
    {
        // your existing code where you try to move a file
    }
    catch (IOException ioEx)
    {
        // This error could happen for a number of reasons...
        // One of them being the file is in use by another process.
    }
}

然后在catch您可以根據需要處理錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM