Click here to Skip to main content
15,354,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm writing a backup program for our new server. I'm an administrator, and I'm learning a lot. The issue is that I can back up a file on the c:\ drive to the c:\ drive, but not for the drives on the SAN, like when I try to backup a file on the T drive (SAN) to the H drive (server). I tried using SetAttributes like
System.UnauthorizedAccessException: Access to the path denied[^]
but it basically gives the same error message to try setAttributes as I did when I tried to copy the file. This is a portion of my log:

12/30/2013 2:14:57 PM Successful backup of file C:\test\iceCreamCake_12_30_2013_1414P.docx
12/30/2013 2:14:57 PM exception during backupSystem.UnauthorizedAccessException: Access to the path 'T:\T Drive.vhd' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.SetAttributes(String path, FileAttributes fileAttributes)
   at Bak.BackItUp(String fromDrive, String toDrive) in C:\Users\michele\BackupProj\ServerBackup\ServerBackup\Backup.cs:line 36
12/30/2013 2:14:57 PM exception during backupSystem.UnauthorizedAccessException: Access to the path 'S:\SQL Database.vhd' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.SetAttributes(String path, FileAttributes fileAttributes)
   at Bak.BackItUp(String fromDrive, String toDrive) in C:\Users\michele\BackupProj\ServerBackup\ServerBackup\Backup.cs:line 36


Shouldn't I be able to run my program to do the backup if I'm logged on as Administrator?

Here's part of the code:

C#
try
{
    if (System.IO.File.Exists(fromDrive))
    {
        result = 4;
        if (System.IO.Directory.Exists(toDrive))
        {
            string oldFileName = Path.GetFileName(fromDrive); //file name only
            string sourcePath = Path.GetDirectoryName(fromDrive); //path only
            string newFileName = AppendFileNameWithDate(oldFileName); //file name with date added
            string destFile = System.IO.Path.Combine(toDrive, newFileName); //full path of final destination with new file name
            result = 3;

            System.IO.File.SetAttributes(fromDrive, FileAttributes.Normal);
            System.IO.File.Copy(fromDrive, destFile, true);  //copy backupFileName to toDrive, overwrite destination file if it already exists
            if (File.Exists(destFile))
            {
                Logging.Logging.Instance.Debug("Successful backup of file " + destFile);
                result = 2;
            }
            else
            {
                result = -2;
                Logging.Logging.Instance.Debug("Backup *failure of file " + destFile);
            }

        }
        else
        {
            Logging.Logging.Instance.Debug("to Drive does not exist: " + toDrive);
            result = -1;
        }
    }
    else
    {
        Logging.Logging.Instance.Debug("from Drive does not exist: " + fromDrive);
        result = -1;
    }
}
catch (Exception ex)
{
    Logging.Logging.Instance.Debug("exception during backup" + ex.ToString());
}


The directory strings are like this:

C#
string cDrive  = @"C:\backup\2013\iceCreamCake.docx";
string tDrive  =  @"T:\T Drive.vhd";
string sDrive  =  @"S:\SQL Database.vhd";

string cDriveToLocation = @"C:\test";
string tDriveToLocation = @"H:\";
string sDriveToLocation = @"E:\";
string vDriveToLocation = @"G:\";


The SAN is set up as SAS (not network share or iSCSI), so it is the same as a physically connected hard drive. We were able to open a command window and could xcopy the file on the t-drive to the h-drive, so it's just the code that can't do the copy/recognize the path. I tried changing the filename to @"T:\T_Drive.vhd", and also tried changing the FileAttributes line to: System.IO.File.SetAttributes(fromDrive, FileAttributes.System); It still gives the same error message. It looks like it can't access the path to the file.

I looked at these links but I'm having newbie issues too I think:
http://stackoverflow.com/questions/1267085/vista-uac-trouble-mapping-network-drives[^]

http://stackoverflow.com/questions/8821410/system-unauthorizedaccessexception-access-to-the-path-denied?lq=1[^]

http://stackoverflow.com/questions/6402536/access-to-the-path-xxx-is-denied?rq=1[^]

Thanks,
Michele
Posted
Updated 7-Jan-14 4:17am
v4

1 solution

I'm going to take a guess and say you might need to use the UNC path vs. the mapped path.

Here instead of Z:\PlaceIWantToAccess we often need to use \\TheServer\PlaceIWantToAccess
   
Comments
MichCl 7-Jan-14 10:12am
   
By \\TheServer do you mean the IP address or what?
bowlturner 7-Jan-14 10:20am
   
no while the IP can work I wouldn't use it. Use the name of the server and the share path. Look up in the properties of the share at the path. \\ServerName\Share
MichCl 7-Jan-14 14:16pm
   
We don't have our server set up as a share. I'm not sure how to access it with server name \\USA0261xxxxx without the drive letter.
bowlturner 7-Jan-14 14:24pm
   
if it's mapped and accessible, it is shared. Right click on the T: drive and go to properties. In Windows 7 there is a DFS tab. it will show you the UNC path, you might also ask the server administrator for it as well.
MichCl 7-Jan-14 14:43pm
   
I am the administrator, but we are all learning. When I go to T:\ properties, it says not shared. I can go to Advanced Sharing and give it a share name, but shouldn't I already have access since I'm logged onto the server directly? Plus I can open a command window and cd t:
bowlturner 7-Jan-14 14:59pm
   
New thought. I am thinking that the 'user' the process is running under doesn't have access to the T: drive. Do you know who/what it is running as? Find out (I think you can through the Task Manager) and make sure that account has access to the T: drive. At this point that makes the most sense to me. You as the admin have access, but the process doesn't.
MichCl 7-Jan-14 16:09pm
   
The 'user' the process is running under (from Task Manager) is my username/login (usxxxxxx). C:\ file copied ok but T:\ etc didn't. I was then looking into what my classification was. I'm not sure now that I'm Administrators logged in with that (usxxxxxxx). I'll have to check with AD people. On the server, we only list giving Administrators rights to my other login that we created on the server (M_Cl). When I login as M_Cl, the C:\ file doesn't copy, with same error message as the t:\ file.

I looked and Administrators have Full Control (modify, read/exec, read, write, etc) of t:\. Users priviledges of t:\ are Read & Execute, create folders.

On the other hand, C:\ gives full control to Administrators, and Users priviledges are Read & Execute, create folders. (same)

It's very confusing what's going on here. I think it has to do with priviledges, but the c:\ file copies with using presumably user priviledges but not Administrators priviledges (but the Administrators have more priviledges).
bowlturner 7-Jan-14 16:43pm
   
Interesting. I agree it is a privileges thing but this is not my area of expertise. If I was looking over your shoulder I might see the problem, but as it is...
MichCl 8-Jan-14 10:20am
   
Thanks for all your help. Remind me how I can give you points for your help on this site? I wound up specifically adding each of my logins (usxxxxxx and M_CL) to the properties of the files/directories, then editing the privileges to get full control, and also removing all spaces in the file for the t:\ drive file and the c:\ directories file (it was failing for my M_Cl login but not usxxxx), and it worked for c:\ and t:\. The other thing I added was instead of having the complete string for t drive defined with file, I did a path combine like string sDrive = Path.Combine(@"S:\", @"SQL_Database.vhd");
bowlturner 8-Jan-14 11:27am
   
you can give me a 5 star on my answer and/or mark click the 'Accept Solution' button.

Glad I helped some, more importantly glad you got it to work!. I was afraid it might involve something like your final solution, but there is no way I would have been able to talk you through it.

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