开发者

querying a directory for files that are complete (not copy-in-progress)

开发者 https://www.devze.com 2023-04-11 01:22 出处:网络
I tried to use FileInfo.CreationTime, but it doesn\'t represent the copy finish time. I am trying to get a list of files in a directory. The problem is that the call also returns files which are not

I tried to use FileInfo.CreationTime, but it doesn't represent the copy finish time.

I am trying to get a list of files in a directory. The problem is that the call also returns files which are not yet finished copying.

If I try to use the file, it returns an error stating that the file is in use.

How can you query for files that are fully copied?

As below code. Directory.GetFiles() returns which are not yet finished copying.

my test file size is over 200Mb.

if(String.IsNullOrEmpty(strDirectoryPath)){

                txtResultPrint.AppendText("ERROR : Wrong Directory Name! ");
            }else{
  开发者_JS百科              string[] newFiles = Directory.GetFiles(strDirectoryPath,"*.epk");

                _epkList.PushNewFileList(newFiles);

                if(_epkList.IsNewFileAdded()){
                    foreach (var fileName in _epkList.GetNewlyAddedFile()){
                        txtResultPrint.AppendText(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "   => ");
                        txtResultPrint.AppendText(fileName + Environment.NewLine);
                        this.Visible = true;
                        notifyIconMain.Visible = true;
                    }

                }else{

                }
            }


If performance and best-practices aren't huge concerns then you could simply wrap the failing file operation in an inner-scoped try/catch.

 using System.IO;
 string[] files = Directory.GetFiles("pathToFiles");
 foreach (string file in files) {
     FileStream fs = null;
     try {
         //try to open file for exclusive access
         fs = new FileStream(
             file, 
             FileMode.Open, 
             FileAccess.Read, //we might not have Read/Write privileges
             FileShare.None   //request exclusive (non-shared) access
         );
     } 
     catch (IOException ioe) {
         //File is in use by another process, or doesn't exist
     }
     finally {
         if (fs != null)
             fs.Close();
     }
 }

This isn't really the best design advice as you shouldn't be relying on exception handling for this sort of thing, but if you're in a pinch and it's not code for a client or for your boss then this should work alright until a better solution is suggested or found.


Do you have the ability to change thy copying itself?

If yes (and if you can guarantee that your program will always execute on NTFS on Windows Vista or newer), you can use Transactional NTFS to wrap the copy in a single transaction. File(s) being copied will only become visible to the rest of the world after you commit the transaction, so you'll never even see the partially copied files.

Unfortunately Transactional NTFS is not accessible directly from .NET Framework - you'll need to P/Invoke into Win32 APi functions such as: CreateTransaction, CommitTransaction, RollbackTransaction, CopyFileTransacted (and other *Transacted functions).

0

精彩评论

暂无评论...
验证码 换一张
取 消