gpt4 book ai didi

c# - 在目录中查询完整的文件(不是正在进行的复制)

转载 作者:太空狗 更新时间:2023-10-29 23:41:15 26 4
gpt4 key购买 nike

我尝试使用FileInfo.CreationTime,但它并不代表复制完成时间。

我正在尝试获取目录中的文件列表。问题是该调用还返回尚未完成复制的文件。

如果我尝试使用该文件,它会返回一个错误,指出该文件正在使用中。

如何查询已完全复制的文件?

如下代码。 Directory.GetFiles() 返回尚未完成复制的内容。

我的测试文件大小超过 200Mb。

if(String.IsNullOrEmpty(strDirectoryPath)){

txtResultPrint.AppendText("ERROR : Wrong Directory Name! ");
}else{
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{

}
}

最佳答案

如果性能和最佳实践不是大问题,那么您可以简单地将失败的文件操作包装在内部范围的 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();
}
}

这并不是最好的设计建议,因为你不应该依赖异常处理来处理这类事情,但如果你手头紧,而且它不是为客户或老板编写的代码,那么这应该继续工作,直到提出或找到更好的解决方案。

关于c# - 在目录中查询完整的文件(不是正在进行的复制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com