Today when I coded a snippet to download file from a team foundation server, I met this error:

UnauthorizedAccessException was unhandled.

Access to the path is denied.

UnauthorizedAccessException was unhandled

The relative code is:

private static void DownloadFile()
{
    TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(@"serverNameOrUrl");
    VersionControlServer vc = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
    Console.WriteLine("Downloading...");
    vc.DownloadFile(@"$/YourPath/YourPath/YourPath/File.aspx", @"C:\test.aspx");
    Console.WriteLine("Done");
    Console.ReadKey();
}

I was confused by this error, and checked my permission to the Team Foundation Server again and again, and found there is no issue with my account connected to the TFS server.

After a long time, I found out the error was not saying that the code has not enough permission to download file from TFS server, it was saying that the code has not enough permission to save the file into local disk!

The Windows 7 OS won’t let code to save file into local disk C: by default. I just changed the disk to D: and the problem was fixed.

//...
vc.DownloadFile(@"$/YourPath/YourPath/YourPath/File.aspx", @"D:\test.aspx");
//...