第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

等待文件完全寫入

等待文件完全寫入

C#
一只斗牛犬 2019-10-10 16:42:42
在一個(gè)目錄中創(chuàng)建(FileSystemWatcher_Created)文件后,我將其復(fù)制到另一個(gè)目錄。但是,當(dāng)我創(chuàng)建一個(gè)大的(> 10MB)文件,它無法復(fù)制的文件,因?yàn)樗鼏訌?fù)制已經(jīng),當(dāng)文件尚未完成創(chuàng)建... 這會導(dǎo)致無法復(fù)制的文件,因?yàn)樗怯闪硪粋€(gè)進(jìn)程使用是提高。;(有幫助嗎?class Program{    static void Main(string[] args)    {        string path = @"D:\levan\FolderListenerTest\ListenedFolder";        FileSystemWatcher listener;         listener = new FileSystemWatcher(path);        listener.Created += new FileSystemEventHandler(listener_Created);        listener.EnableRaisingEvents = true;        while (Console.ReadLine() != "exit") ;    }    public static void listener_Created(object sender, FileSystemEventArgs e)    {        Console.WriteLine                (                    "File Created:\n"                   + "ChangeType: " + e.ChangeType                   + "\nName: " + e.Name                   + "\nFullPath: " + e.FullPath                );        File.Copy(e.FullPath, @"D:\levan\FolderListenerTest\CopiedFilesFolder\" + e.Name);        Console.Read();    }}
查看完整描述

3 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

對于您面臨的問題,只有解決方法。


開始復(fù)制之前,請檢查文件ID是否正在處理中。您可以調(diào)用以下函數(shù),直到獲得False值為止。


第一種方法,直接從此答案復(fù)制:


private bool IsFileLocked(FileInfo file)

{

    FileStream stream = null;


    try

    {

        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);

    }

    catch (IOException)

    {

        //the file is unavailable because it is:

        //still being written to

        //or being processed by another thread

        //or does not exist (has already been processed)

        return true;

    }

    finally

    {

        if (stream != null)

            stream.Close();

    }


    //file is not locked

    return false;

}

第二種方法:


const int ERROR_SHARING_VIOLATION = 32;

const int ERROR_LOCK_VIOLATION = 33;

private bool IsFileLocked(string file)

{

    //check that problem is not in destination file

    if (File.Exists(file) == true)

    {

        FileStream stream = null;

        try

        {

            stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

        }

        catch (Exception ex2)

        {

            //_log.WriteLog(ex2, "Error in checking whether file is locked " + file);

            int errorCode = Marshal.GetHRForException(ex2) & ((1 << 16) - 1);

            if ((ex2 is IOException) && (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION))

            {

                return true;

            }

        }

        finally

        {

            if (stream != null)

                stream.Close();

        }

    }

    return false;

}


查看完整回答
反對 回復(fù) 2019-10-10
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

這是一個(gè)舊線程,但我將為其他人添加一些信息。


我在編寫PDF文件的程序中遇到了類似的問題,有時(shí)它們需要30秒才能呈現(xiàn)..這與我的watcher_FileCreated類在復(fù)制文件之前等待的時(shí)間相同。


文件未鎖定。


在這種情況下,我檢查了PDF的大小,然后等待2秒鐘再比較新的大小,如果它們不相等,則該線程將休眠30秒鐘,然后重試。


查看完整回答
反對 回復(fù) 2019-10-10
  • 3 回答
  • 0 關(guān)注
  • 663 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號