Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted
Updated 18-Jul-17 3:14am
Comments
Soner Gönül 17-Jul-17 4:01am    
On which line you get this exception? What is the full exception by the way?
Member 12363094 17-Jul-17 5:01am    
do not know in local system working fine but in server not working.
Access to the path is destinationFile\filename1.txt has denied.
Description: An unhandled exception occurred during the execution of the current web request.
F-ES Sitecore 17-Jul-17 4:35am    
The account your code is running under (the anonymous user) probably doesn't have the rights to delete the file. Change the rights on the folder so the anonymous account has delete rights or change the anonymous user to one with the required permissions.
Atlapure Ambrish 17-Jul-17 7:12am    
Have checked if the folder/file is read-only on server? if it is, just remove the read-only flag from folder/file properties and see if it works.

1 solution

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;
}

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:
 
Share this answer
 
Comments
George Swan 18-Jul-17 13:46pm    
Is this answer original or have you copied it from
c# - Is there a way to check if a file is in use? - Stack Overflow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900