3

I'm trying to copy files from a client to a server that has a shared drive. When I do that I get the UnauthorizedAccessException and it says access denied to this path I'm trying to copy to. This is very odd since I have all the permissions and I am logged in as the administrator. The weird thing is that when writing to a text file on the same path on the server it works. Like this:

FileStream fs = new FileStream(@"\\SIMON-VAIO\SimonShared\Users\Simon\Desktop\Files\Clients\clients.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        using (StreamReader sr = new StreamReader(fs))
        {
            while (sr.Peek() >= 0) //as long as there are characters to read
            {
                ListViewItem lvwItem = new ListViewItem();
                lvwItem.Text = sr.ReadLine();
                AddListViewItem(lvwItem);
            }
            sr.Close();
        }

But when doing the copy method I get this exception. Here is how I copy:

FileInfo file = new FileInfo(sourcePath);
            string newPath = targetPath + "\\" + file.Name;
            File.Copy(sourcePath, newPath, true);

So I enter the whole path with the filename and extension for both source and target.

I've tried stuff like "File.SetAttributes(dest, FileAttributes.Normal);", and everything on the server, like security on the map etc.

Do you have a clue what's wrong? I would appreciate your help alot!

Thanks in advance

bash.d
  • 13,029
  • 3
  • 29
  • 42
user2099024
  • 1,452
  • 4
  • 16
  • 26
  • 2
    Just sounds like a permissions issue can you check the folder permissions on your local also are you even `Admin` on that machine..? – MethodMan Mar 27 '13 at 15:04
  • 1
    Could the error relate to the SOURCE location? Do you have access to that file / folder? – Belogix Mar 27 '13 at 15:07
  • @ DJKRAZE I have logged in as administrator the shared network, and I've set the permissions to full authority. @ Belogix I never thought of that! I will check tomorrow and update with an answer if that was the case! Thanks – user2099024 Mar 27 '13 at 19:17

3 Answers3

2

You might have read/write permissions for that file in particular but not on the folder itself. So that is one reason why you cannot create a new file at that location.

Also keep in mind that if you have UAC enabled, the application will not "inherit" your admin privileges unless you give them explicitly (by Running as Admin)

dutzu
  • 3,883
  • 13
  • 19
1

That particular issue can occur due to these issues:

  • Invalid Access to Drive
  • Invalid Access to Folder
  • Invalid Access to Overwrite or Move File.

Based on the path location, it would leave me to believe that it is another computer. You would have to ensure that it meets the accessibility requirements that you require to perform your task.

You can accomplish this several different ways:

  • Write a test for proper elevated permission.
  • Attempt to have your program impersonate the Administrator or Network Service Account.
  • You could query the permissions before you attempt to interact with it.

Those are a few ways you could do that.

You could even test against the UAC:

WindowsIdenetity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);

if (Environment.OSVersion.Platform == PlatformId.Win32NT) && (Environment.OSVersion.Version.Major > 6)
{
     if(!(String.NullOrEmpty(user.ToString())))
     {
          if(role.IsInRole(WindowsBuiltInRole.Administrator)
          {
              // Good
          }
     }
}

That would be a way to actually verify your in the proper role; if not then you could actually write a MessageBox or some other means to indicate insufficient permission.

You can even query the directory:

string remotePath = @"\\SomeLocation";
bool IsAccess = false;

DirectoryInfo di = new DirectoryInfo(remotePath);
if(di.Exists)
{
    try
    {
        var acl = di.GetAccessControl();
        IsAccess= true;
    }
    catch(UnauthorizedAccessException ex)
    {
       if(ex.Message.Contains("read-only"))
       {
           IsAccess = true;
       }
       else
       {
           // No Access Fail, do something else.
       }
    }
}

As you can see you have quite a bit of flexibility in this case. Hopefully this helps you a bit.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • As you say the target path is on another computer which i login to as administrator with impersonation over the shared network. This is just for checking I guess. I have found some solutions about the read-only issue that seems to be a problem for many. Something like: 'FileInfo fileInfo = new FileInfo(destinationFile);' 'fileInfo.IsReadOnly = false;' I will try stuff like that, and test debug in your way too tomorrow and I'll write here about the results. Thanks for your answer! – user2099024 Mar 27 '13 at 19:25
  • Not a problem, hopefully it helps. – Greg Mar 27 '13 at 20:14
0

I am so sorry guys but the problem was that I forgot to change the permissions to "full control" when I first shared the drive, and had only read permission. That made it impossible for computers to do anything except for read over the shared network. I'm sorry again, my fault! I really appreciate your help anyway, thanks!

user2099024
  • 1,452
  • 4
  • 16
  • 26