Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.Move(): Access to Path is Denied

I'm writing this Windows Form Application in Visual Studio 2010 using C#.

There is a Execute button on the form, the user will hit the button, the program will generate some files and are stored in the Output folder (which is created by the program using Directory.CreateDirectory())

I want to create an Archive folder to save the output files from previous runs.

In the beginning of each run, I try to move the existing Output folder to the Archive folder, then create a new Output folder. Below is the function I ran to move directory.

static void moveToArchive()
{
    if (!Directory.Exists("Archive")) Directory.CreateDirectory("Archive");
    string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
    try
    {
        Directory.Move("Output", "Archive\\" + timestamp);
    }
    catch(Exception e)
    {
        Console.WriteLine("Can not move folder: " + e.Message);
    }
}

The problem I ran into confuses me a lot...

There are some times that I can successfully move the Output folder to archive, but sometimes it fails.

The error message I got from catching the exception is Access to path 'Output' is denied.

I have checked that all the files in the Output folder are not in use. I don't understand how access is denied sometimes and not all the times.

Can someone explain to me and show me how to resolve the problem?

--Edit--

After HansPassant comment, I modified the function a little to get the current directory and use the full path. However, I'm still having the same issue.

The function now looks like this:

static void moveToArchive()
{
    string currentDir = Environment.CurrentDirectory;
    Console.WriteLine("Current Directory = " + currentDir);
    if (!Directory.Exists(currentDir + "\\Archive")) Directory.CreateDirectory(currentDir + "\\Archive");
    string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
    try
    {
        Directory.Move(currentDir + "\\Output", currentDir + "\\Archive\\" + timestamp);
    }
    catch(Exception e)
    {
        Console.WriteLine("Can not move folder: " + e.Message);
    }
}

I printed out the current directory and it is just as what I was expecting, and I'm still having trouble using full path. Access to path 'C:\Users\Me\Desktop\FormApp\Output' is denied.

--Edit--

Thank you everyone for answering and commenting.

I think some of you miss this part so I'm going stress it a bit more.

The Directory.Move() sometimes work and sometimes fails.

When the function succeed, there was no problem. Output folder is moved to Archive

When the function fails, the exception message I got was Access to path denied.

like image 630
sora0419 Avatar asked Sep 20 '13 19:09

sora0419


2 Answers

Thank you all for the replies and help. I have figured out what the issue was.

It is because there was a file that's not completely closed.

I was checking the files that were generated, and missed the files the program was reading from.

All files that were generated were closed completely. It was one file I used StreamReader to open but didn't close. I modified the code and am now not having problem, so I figure that's were the issue was.

Thanks for all the comments and answers, that definitely help me with thinking and figuring out the problem.

like image 91
sora0419 Avatar answered Sep 24 '22 19:09

sora0419


File.SetAttributes(Application.dataPath + "/script", FileAttributes.Normal);
Directory.Move(Application.dataPath + "/script", Application.dataPath + "/../script");

This fixed my problem.

like image 24
blueriver123 Avatar answered Sep 25 '22 19:09

blueriver123