7

So I've looked for the answer multiple times, and meddled around with permission stuff to no avail, but this code still won't let me download a file to a specified path.

WebClient client = new WebClient();
client.DownLoadFile("http://dl.dropbox.com/u/(My account)/Installer.jar", @"c:\Games\Name\libs");
client.DownLoadFile("http://dl.dropbox.com/u/(My account)/Name.zip", @"c:\Games\Name");

Always gives me: "Access to the path 'c:\Games\Name\libs' is denied."

Also note, using windows XP SP3.

Fishrock123
  • 113
  • 2
  • 7

4 Answers4

11

Hi I tried the above code locally and got the same error "Access is denied":

WebClient myWebClient = new WebClient();
    myWebClient.DownloadFile("http://localhost:1929/2.png", @"C:\Z\)

Try specifying the filename at the end of the directory, saves locally no problem when i ran it:

WebClient myWebClient = new WebClient();
    myWebClient.DownloadFile("http://localhost:1929/2.png", @"C:\Z\FILENAME.jpg")
invulnarable27
  • 591
  • 5
  • 22
2

The App probably doesn't have the permission to write to that folder. If this is a client app, try to run it as an administrator. Otherwise, change the permissions on 'c:\Games\Name\libs' to full control for everyone.

tzerb
  • 1,433
  • 1
  • 16
  • 35
  • Correct, I just don't know how to make it prompt for permission or anything, I guess. Also I am the admin on my computer, so that's just weird. In addition to that. I don't seem to be able to disable the "read-only" attribute on the folders.. O_o – Fishrock123 Apr 29 '12 at 03:42
  • Right click on the folder and click properties. Look for the security tab. – tzerb Apr 29 '12 at 05:22
  • Win XP SP3 doesn't have that tab... And I don't want the people using my program to have to do it via properties. >_ – Fishrock123 Apr 29 '12 at 20:32
  • If only I was sitting at your computer. – tzerb Apr 29 '12 at 20:42
1

If the access to there is denied try to run as administrator.

If it doesn't work, navigate to the folder C:\Games\Name\libs, right click it and go to Properties. Choose the "Security" tab, select at the top list the group of users that will run your program. (Try to use Users (YourName-PC\Users)). With it selected, click on Edit at the bottom of the list, and at the bottom list select Full control under Allow.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • That is not a dynamic solution that should work for anyone in the organisation. – Fandango68 Nov 12 '19 at 01:47
  • @Fandango68 true but that if that's your setting and the system admins won't give you filesystem access vital for work then you've got much bigger problems than the one in the OP. – MasterMastic Nov 12 '19 at 19:57
0

you may use code below to see if you have write permission on folder, if not set the failing rule using setaccesscontrol before downloading

 public static bool HaveWritePermissionsForFolder(string path)
    {
        var rules = Directory.GetAccessControl(Path.GetDirectoryName(Path.GetDirectoryName(path))).GetAccessRules(true, true, typeof(SecurityIdentifier));

        bool allowwrite = false;
        bool denywrite = false;
        foreach (FileSystemAccessRule rule in rules)
        {
            if (rule.AccessControlType == AccessControlType.Deny &&
                (rule.FileSystemRights & FileSystemRights.WriteData) == FileSystemRights.WriteData &&
                (groups.Contains(rule.IdentityReference) || rule.IdentityReference.Value == sidCurrentUser)
                )
            {
                denywrite = true;
            }
            if (rule.AccessControlType == AccessControlType.Allow &&
                (rule.FileSystemRights & FileSystemRights.WriteData) == FileSystemRights.WriteData &&
                (groups.Contains(rule.IdentityReference) || rule.IdentityReference.Value == sidCurrentUser)
                )
            {
                allowwrite = true;
            }
        }


        if (allowwrite && !denywrite)
            return true;

        return false;
    }
Brijesh Mishra
  • 2,738
  • 1
  • 21
  • 36