Assaf Rabinowitz
Assaf Rabinowitz

Reputation: 63

Access to the path is denied - C#

I have written a program that copies files. Sometimes, while trying to copy a file an exception is thrown - "Access to the path ... is denied". (Probably because another program uses this file).

Notice: I run the program as an administrator

BUT! when I copy the same file manually it works!

Why? What is wrong with the program?

try
                {
                    CopyClass.Copy(m_FilesSources[i].Path, m_FilesDestinations[i], true, m_FilesSources[i].Postfix);
                }
catch (Exception ex)
                {
                    isAccessDenied = true;
                    tbLog.Text += " - " + ex.Message + "\n";
                }


class CopyClass
{
    public static bool Copy(string sourceDirPath, string destDirPath, bool copySubDirs, string postfix)
    {
            if (postfix == null)
            {
                FileCopy(sourceDirPath, destDirPath, copySubDirs);
                return true;
            }
            DirectoryCopy(sourceDirPath, destDirPath, copySubDirs, postfix);
            return true;
    }

    public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, string postfix)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        // If the destination directory doesn't exist, create it. 
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
            {
                string temppath = System.IO.Path.Combine(destDirName, file.Name);
                if (postfix == ".")
                {
                    file.CopyTo(temppath, copySubDirs);
                }
                else if (file.Name.EndsWith(postfix))
                {
                    file.CopyTo(temppath, copySubDirs);
                }
            }

        // If copying subdirectories, copy them and their contents to new location. 
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string tempPath = System.IO.Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, tempPath, copySubDirs, postfix);
            }
        }
    }

    public static void FileCopy(string sourceFileName, string destDirName, bool overwrite)
    {
        string destFileName = destDirName + sourceFileName.Substring(sourceFileName.LastIndexOf('\\') + 1);

        // If the destination directory doesn't exist, create it. 
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }
        System.IO.File.Copy(sourceFileName, destFileName, overwrite);
    }
}

}

Upvotes: 2

Views: 2198

Answers (2)

Assaf Rabinowitz
Assaf Rabinowitz

Reputation: 63

The problem was that the file I had tried to overwrite was read-only. To solve this problem I've manually changed the attributes of the destination file to normal (not read-only):

if (File.Exists(destFileName))
            {
                File.SetAttributes(destFileName, FileAttributes.Normal);           // Makes every read-only file into a RW file (in order to prevent "access denied" error)
            }

Hope this is helpful.

Upvotes: 3

Mark Segal
Mark Segal

Reputation: 5560

It is very likely that you are trying to modify/write/read a file that is inaccessible to the application because the application is ran under a restricted user.

Try running the program as administrator. (Right click -> Run as Administrator).

You can also run VS as an administrator, this will force the application to also run as administrator.

Upvotes: 0

Related Questions