C#下载器调试模式正常,独立运行生成0字节文件且提示系统不兼容
C#下载器调试正常但独立运行异常问题
我开发了一个小型C#下载器,用于下载Python安装包,后续会扩展更多功能。目前遇到问题:在Visual Studio调试模式下,下载功能正常;但直接运行Debug文件夹中的exe文件时,虽能加载更新界面,却生成0字节的文件,且尝试运行该文件时提示“此程序与你的操作系统不兼容”。
相关代码
private string GetFileName(string url) { string[] parts = url.Split('/'); string fileName = ""; if (parts.Length > 0) fileName = parts[parts.Length - 1]; else fileName = url; return fileName; } private String getFileName(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); string fileName = ""; try { HttpWebResponse res = (HttpWebResponse)request.GetResponse(); using (Stream rstream = res.GetResponseStream()) { fileName = res.Headers["Content-Disposition"] != null ? res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace("\"", "") : res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) : Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ? Path.GetFileName(res.ResponseUri.ToString()) : GetFileName(url); } res.Close(); } catch { } return fileName; } public async Task downloadupdate() { if(os == "x86") { filename = getFileName("https://www.python.org/ftp/python/3.11.0/python-3.11.0-amd64.exe"); string url = "https://www.python.org/ftp/python/3.11.0/python-3.11.0-amd64.exe"; lblfilename.Text = "File Name: " + getFileName(url); using (WebClient wc = new WebClient()) { wc.DownloadFileCompleted += wc_completed; wc.DownloadProgressChanged += wc_DownloadProgressChanged; XtraMessageBox.Show(filename); filename = "python.exe"; wc.DownloadFileAsync(new Uri(url), "C://Temp" + "//" + filename); } } else { filename = getFileName("https://www.python.org/ftp/python/3.11.0/python-3.11.0.exe"); string url = "https://www.python.org/ftp/python/3.11.0/python-3.11.0.exe"; lblfilename.Text = "File Name: " + getFileName(url); using (WebClient wc = new WebClient()) { wc.DownloadFileCompleted += wc_completed; wc.DownloadProgressChanged += wc_DownloadProgressChanged; wc.DownloadFileAsync(new Uri(url), "C://Temp" + "//" + filename); } } } private void wc_completed(object sender, AsyncCompletedEventArgs e) { _MessageResult.Instance.Append("Download Completed!"); //unzipupdate(Tiaga_Res.unzipupdate = "fixes19.zip"); //DirectoryCopy(Tiaga_Res.startpath + "\\fixes19\\", Tiaga_Res._bo3_root, true); //XtraMessageBox.Show("Applied Fix Aug And Reflex Sight Fix"); // richTextBox1.Text = _MessageResult.Instance.Merge(); Process.Start("C://Temp" + "//" + filename); this.Close(); } void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBarControl1.Properties.Step = 1; progressBarControl1.Properties.PercentView = true; //progressBarControl1.Properties.Maximum = e.ProgressPercentage; progressBarControl1.PerformStep(); UpdateProgressBar(e.ProgressPercentage); txtpercent.Text = e.ProgressPercentage + "%"; double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); lbldownloaded.Text = "Downloaded: " + bytesIn + "/" + totalBytes; } private void UpdateProgressBar(int value) { progressBarControl1.EditValue = value; }
调试模式截图
- 调试模式下文件名生成:

- 调试模式下文件下载中:

- 独立运行模式下文件运行失败:

我认为不是libz的问题,因为我一直用它编译DLL到项目中,从未出现过此类问题。
内容的提问来源于stack exchange,提问作者elfenliedtopfan5
相关产品推荐
相关产品推荐

