开发者

file cannot be deleted used by another process

开发者 https://www.devze.com 2022-12-14 16:38 出处:网络
when ever i try to delete this filessome times i get this message file cannot bedeletedused by another process

when ever i try to delete this files some times i get this message file cannot be deleted used by another process even though file is not used by any other user the code what i am using

try 
{

FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(".") + "\\" + txtFile.Text);
} 
else {

throw new FileNotFoundException();
}
}

catch (FileNotFoundException ex) {

lblStatus.Text += ex.Message;
}

is there way we can assign a boolean value

here开发者_StackOverflow if true means i can display an message tellingfile is deleted successfully boolean value is false here if false means i can display an message telling file used by another process

can i write that condition here File.Delete(MapPath(".") + "\\" + txtFile.Text); any help would be great thank you


Based on my experience, if it says the file is being used by another process, the file is being used by another process, whatever you think...

Download ProcessExplorer to find out who the culprit is...


You can use the

FileInfo.Delete

method. If the file does not exist, this method does nothing.

Note

Also check for any other file handles that has locked this file, close them before deleting the file.


A bit late to the party I know, but hopefully these comments will help others out ->

The issue is not happening in Chrome, but is in IE6/7/8 and FF. This method is working for me now in those latter browsers:

                    try
                    {

                        System.IO.File.Delete(basePath + assetImage.Path);
                    }
                    catch 
                    {
                        System.IO.File.Move(basePath + assetImage.Path, basePath + @"DELETE\ " + assetImage.Path);
                    }


Why don't you try

if (TheFile.Exists) {
   TheFile.Delete();
}

?


If the Indexing Service is turned on, make sure this folder is excluded from the Index list.


If you want a method which just returns true if the file was deleted successfully, and false otherwise, I'd go with:

        public bool DeleteFile(string fullPath) {
            bool fileDeleted = false;

            if (System.IO.File.Exists(fullPath))
            {
                try
                {
                    System.IO.File.Delete(fullPath);
                    fileDeleted = true;
                }
                catch (UnauthorizedAccessException)
                {
                    // File is open **or read only**
                }
            }
            else
            {
                // File does not exist
            }

            return fileDeleted;
        }
0

精彩评论

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