C# 拒绝访问AppDomain.CurrentDomain.BaseDirectory

C# 拒绝访问AppDomain.CurrentDomain.BaseDirectory,c#,download,filesystems,C#,Download,Filesystems,我创建了一个具有更新系统的应用程序,这是一个非传统的应用程序: 此应用程序安装和运行于: C:\Program Files (x86)\Application test 可执行文件名为: searchdata.exe 但在某个时候,我想出了一个很棒的主意,制作一个更新系统,您可以通过以下步骤来完成: 单击更新按钮:检查update.exe可执行文件是否存在 如果存在,请立即执行,并关闭searchdata.exe 它不存在:从url下载update.dat,然后重命名为update.exe

我创建了一个具有更新系统的应用程序,这是一个非传统的应用程序:

此应用程序安装和运行于:

C:\Program Files (x86)\Application test
可执行文件名为:

searchdata.exe
但在某个时候,我想出了一个很棒的主意,制作一个更新系统,您可以通过以下步骤来完成:

  • 单击更新按钮:检查
    update.exe
    可执行文件是否存在
    • 如果存在,请立即执行,并关闭
      searchdata.exe
    • 它不存在:从url下载
      update.dat
      ,然后重命名为update.exe
  • 这是下载和进度条脚本:
  • private void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
    {
    下载文件WithProgress(“http://test.pro/installer/update.dat,AppDomain.CurrentDomain.BaseDirectory+“update.exe”,ProgressBar1,Label4);
    }
    私有void DownloadFileWithProgress(字符串DownloadLink、字符串TargetPath、ProgressBar Progress、标签LabelProgress)
    {
    int字节数=0;
    Stream remoteStream=null;
    Stream localStream=null;
    HttpWebResponse响应=null;
    尝试
    {
    WebRequest=WebRequest.Create(DownloadLink);
    double TotalBytesToReceive=0;
    var SizewebRequest=WebRequest.Create(新Uri(下载链接));
    SizewebRequest.Method=“HEAD”;
    使用(var WebResponse=SizewebRequest.GetResponse())
    {
    var fileSize=WebResponse.Headers.Get(“内容长度”);
    TotalBytesToReceive=Convert.ToDouble(文件大小);
    }
    response=(HttpWebResponse)request.GetResponse();
    if(response.StatusCode==HttpStatusCode.OK)
    {
    remoteStream=response.GetResponseStream();
    字符串filePath=TargetPath;
    localStream=File.Create(filePath);
    字节[]缓冲区=新字节[1024];
    int字节读取=0;
    做
    {
    bytesRead=remoteStream.Read(buffer,0,buffer.Length);
    写(缓冲区,0,字节读);
    字节处理+=字节读取;
    double bytesIn=double.Parse(bytesProcessed.ToString());
    双百分比=字节数/总字节数或接收*100;
    百分比=数学四舍五入(百分比,0);
    if(Progress.invokererequired)
    {
    Progress.Invoke(新方法调用程序(()=>
    {
    Progress.Value=int.Parse(Math.Truncate(percentage.ToString());
    }));
    }
    其他的
    {
    Progress.Value=int.Parse(Math.Truncate(percentage.ToString());
    }
    if(LabelProgress.invokererequired)
    {
    LabelProgress.Invoke(新方法调用程序(()=>
    {
    LabelProgress.Text=int.Parse(Math.Truncate(percentage).ToString())+“%”;
    }));
    }
    其他的
    {
    LabelProgress.Text=int.Parse(Math.Truncate(percentage).ToString()).ToString()+“%”;
    }
    }
    而(字节读取>0);
    }
    其他的
    {
    抛出新ArgumentException(response.StatusDescription,“Respuesta del Servidor”);
    }
    }
    捕获(例外情况除外)
    {
    MessageBox.Show(“下载文件时出错,进度”+ex.Message);
    }
    最后
    {
    if(响应!=null)
    response.Close();
    if(remoteStream!=null)
    remoteStream.Close();
    if(localStream!=null)
    localStream.Close();
    }
    }
    
  • 问题是,我是以正常权限运行的,我不想使用管理权限,但在尝试将文件写入应用程序目录时,执行会给我一个拒绝访问错误

  • 我感谢你对第3点的帮助,如果你能看到脚本,我不知道你能给我带来什么改进


  • 默认情况下,您正在使用的program files文件夹需要额外的权限。您可以考虑使用AppDATE文件夹,而默认权限应该是足够的。@ OLabBACK您可以给出一个答案,该脚本使用“<代码> AppDATAs/COD> >脚本来查看,我想 Update .exe < /代码>还需要代码更新,因为
    update.exe
    负责下载新版本的
    searchdata.exe