You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebClient下载文件提示拒绝访问问题求助

解决文件夹写入权限拒绝问题

先看你提供的代码:

private void sendmsg_Click(object sender, EventArgs e)
{       
    if (string.IsNullOrEmpty(this.TextBox1.Text))
        Environment.Exit(0);
    openFileDialog1.FileName = this.TextBox1.Text + "\\Unturned_BE.exe";
    System.Diagnostics.Process.Start(openFileDialog1.FileName);
    WebClient webClient = new WebClient();
    webClient.DownloadFile("https://cdn.discordapp.com/attachments/1134788916642336910/1134789171848953927/Rxl.dll", this.TextBox1.Text + "\\Unturned_Data\\Managed");
            
    Environment.Exit(0);
}

核心问题与解决步骤

1. 修正下载路径错误

WebClient.DownloadFile的第二个参数必须是完整文件名,你现在只写了文件夹路径...\\Managed,这会直接导致写入失败。改成:

webClient.DownloadFile("https://cdn.discordapp.com/attachments/1134788916642336910/1134789171848953927/Rxl.dll", 
    this.TextBox1.Text + "\\Unturned_Data\\Managed\\Rxl.dll");

更规范的写法是用Path.Combine拼接路径,避免斜杠兼容问题:

var saveFilePath = Path.Combine(this.TextBox1.Text, "Unturned_Data", "Managed", "Rxl.dll");

2. 确保目标文件夹存在

下载前先创建目标文件夹,避免因文件夹不存在触发的权限类报错:

Directory.CreateDirectory(Path.GetDirectoryName(saveFilePath));

3. 处理系统权限保护

即使以管理员身份运行,Windows受保护目录(如C:\Program Files、C:\Windows)仍会阻止写入,建议:

  • 把游戏安装到自定义路径(如D:\Games\Unturned),避开系统保护目录
  • 若必须用系统目录,在程序启动时强制请求管理员权限:
    在Program.cs中添加权限检查代码:
    static void Main()
    {
        var identity = WindowsIdentity.GetCurrent();
        var principal = new WindowsPrincipal(identity);
        if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
        {
            var startInfo = new ProcessStartInfo(Application.ExecutablePath)
            {
                Verb = "runas",
                UseShellExecute = true
            };
            try
            {
                Process.Start(startInfo);
                return;
            }
            catch (Win32Exception)
            {
                MessageBox.Show("需要管理员权限才能执行操作");
                return;
            }
        }
        
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new YourForm());
    }
    

4. 排查杀毒软件拦截

多数杀毒软件会将未知程序写入游戏目录判定为恶意行为,可暂时关闭杀毒软件或把程序/目标文件夹加入白名单测试。

5. 捕获详细异常定位问题

在代码中添加try-catch,输出具体错误信息,方便精准排查:

private void sendmsg_Click(object sender, EventArgs e)
{       
    if (string.IsNullOrEmpty(this.TextBox1.Text))
    {
        MessageBox.Show("请输入游戏路径");
        return;
    }
    
    try
    {
        var exePath = Path.Combine(this.TextBox1.Text, "Unturned_BE.exe");
        if (!File.Exists(exePath))
        {
            MessageBox.Show("未找到Unturned_BE.exe");
            return;
        }
        Process.Start(exePath);
        
        var saveFilePath = Path.Combine(this.TextBox1.Text, "Unturned_Data", "Managed", "Rxl.dll");
        Directory.CreateDirectory(Path.GetDirectoryName(saveFilePath));
        
        using (WebClient webClient = new WebClient())
        {
            webClient.DownloadFile("https://cdn.discordapp.com/attachments/1134788916642336910/1134789171848953927/Rxl.dll", saveFilePath);
        }
        
        MessageBox.Show("操作完成");
    }
    catch (Exception ex)
    {
        MessageBox.Show($"错误:{ex.Message}\n详细信息:{ex.ToString()}");
    }
}

内容的提问来源于stack exchange,提问作者Hamster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:03:41