如何在删除该文件之前检查文件是否正在使用中。 [英] How to check if file is in use before deleting that file.
问题描述
其实我想将文件从一个文件夹复制到另一个文件夹,但在复制文件之前我必须检查目标文件夹中是否存在相同的文件,如果它存在,我必须删除该文件,然后将新文件复制到目标文件夹。
但我在服务器中收到未经授权的异常。我的错误是因为
i正在删除文件时正在使用,但我不知道发生什么事情。
string filename =file1.txt;
string filename1 =file1Copy.txt ;
string filesource = Server.MapPath(〜/ Lucky /)+ filename;
string destinationFile = Server.MapPath(〜/ LuckyCopy /)+ filename1 ;
if(System.IO.File.Exists(destinationFile))
{
System.IO .File.SetAttributes(destinationFile,FileAttributes.Normal);
System.IO.File.Delete(destinationFile);
} $ / $
System.IO.File.Copy(filesource,destinationFile,true);
System.IO.File.SetAttributes(destinationFile, FileAttributes.Normal);
我的尝试:
i有试过像
Actually i want to copy file from one folder to another folder,but before copying file i have to check same file is exists in destination folder,if it exists first i have to delete that file and then copy new file to destination folder.
but i am getting unauthorized exception in server.i am thing this error is because of
i am deleting file when that is in use,but i donot know excatly whats happening.
string filename = "file1.txt";
string filename1 = "file1Copy.txt";
string filesource = Server.MapPath("~/Lucky/") + filename;
string destinationFile = Server.MapPath("~/LuckyCopy/") + filename1;
if (System.IO.File.Exists(destinationFile))
{
System.IO.File.SetAttributes(destinationFile, FileAttributes.Normal);
System.IO.File.Delete(destinationFile);
}
System.IO.File.Copy(filesource, destinationFile, true);
System.IO.File.SetAttributes(destinationFile, FileAttributes.Normal);
What I have tried:
i have tried like
string filename = "file1.txt";
string filename1 = "file1Copy.txt";
string filesource = Server.MapPath("~/Lucky/") + filename;
string destinationFile = Server.MapPath("~/LuckyCopy/") + filename1;
if (System.IO.File.Exists(destinationFile))
{
System.IO.File.SetAttributes(destinationFile, FileAttributes.Normal);
System.IO.File.Delete(destinationFile);
}
System.IO.File.Copy(filesource, destinationFile, true);
System.IO.File.SetAttributes(destinationFile, FileAttributes.Normal);
推荐答案
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.Read, 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;
}
使用FileAccess.ReadWrite检查对于只读文件将失败,因此已修改解决方案以检查FileAccess.Read。虽然此解决方案有效,因为如果文件上有写入或读取锁定,尝试使用FileAccess.Read将失败,但是,如果文件上没有写入或读取锁定,则此解决方案将无效,即已经使用FileShare.Read或FileShare.Write访问打开(用于读取或写入)。
原文:过去几年我使用过此代码,并且我没有遇到任何问题。
了解你对使用异常的犹豫,但你无法一直避免这些:
Checking with FileAccess.ReadWrite will fail for Read-Only files so the solution has been modified to check with FileAccess.Read. While this solution works because trying to check with FileAccess.Read will fail if the file has a Write or Read lock on it, however, this solution will not work if the file doesn't have a Write or Read lock on it, i.e. it has been opened (for reading or writing) with FileShare.Read or FileShare.Write access.
ORIGINAL: I've used this code for the past several years, and I haven't had any issues with it.
Understand your hesitation about using exceptions, but you can't avoid them all of the time:
这篇关于如何在删除该文件之前检查文件是否正在使用中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!