7

所以我已经多次寻找答案,并且插手许可的东西无济于事,但是这段代码仍然不允许我将文件下载到指定的路径。

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");

总是给我:“访问路径 'c:\Games\Name\libs' 被拒绝。”

另请注意,使用 windows XP SP3。

4

4 回答 4

11

嗨,我在本地尝试了上面的代码并得到了同样的错误“访问被拒绝”:

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

尝试在目录末尾指定文件名,运行时在本地保存没问题:

WebClient myWebClient = new WebClient();
    myWebClient.DownloadFile("http://localhost:1929/2.png", @"C:\Z\FILENAME.jpg")
于 2013-01-29T23:16:23.680 回答
2

该应用程序可能没有写入该文件夹的权限。如果这是客户端应用程序,请尝试以管理员身份运行它。否则,将 'c:\Games\Name\libs' 的权限更改为所有人的完全控制。

于 2012-04-29T03:41:01.297 回答
1

如果对那里的访问被拒绝,请尝试以管理员身份运行。

如果它不起作用,请导航到文件夹C:\Games\Name\libs,右键单击它并转到属性。选择“安全”选项卡,在顶部列表中选择将运行您的程序的用户组。(尝试使用Users (YourName-PC\Users))。Full control选中它后,单击列表底部的编辑,然后在底部列表中选择Allow

于 2012-04-29T03:52:48.447 回答
0

您可以使用下面的代码查看您是否对文件夹有写权限,如果没有在下载之前使用 setaccesscontrol 设置失败规则

 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;
    }
于 2012-04-29T04:23:45.577 回答