问题描述
我正在尝试编写一个小应用程序,由于我只是一名学生,我想知道是否有人可以帮助我提供一些建议/技巧以实现以下目标:
在启动时,所需的程序应调用一个特殊函数。如果有可用更新,该功能会查看远程服务器(很可能是 NAS - 网络附加存储 - 具有给定的用户名 + 密码)。如果是,请下载并安装(包括显示当前进度),然后重新启动程序。
经过一些研究,我找到了以下代码片段,希望对您有所帮助:
var webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync("https://195.50.139.130:5001/FTP/KlingGastro Update/file.txt","C:\\file.txt");
请注意 KlingGastro 和 Update 之间有一个空白字符。 此外,凭据是“KlingGastro”作为用户名,请原谅出于安全原因我不会发布密码。让我们说它的“我的密码”。
我认为下载不会是那么大的问题,我已经找到了这个教程: https://ourcodeworld.com/articles/read/227/how-to-download-a-webfile-with-csharp-and-show-download-progress-synchronously-and-asynchronously 而是自行取消程序并重新启动可能会很困难?
此外,对于所有潜在的语言错误,我们深表歉意,但希望它在某种程度上是可以理解的。 对每一个回答和帮助努力都会感到非常高兴。
最好的问候
解决方法
在 DownloadFileCompleted 事件处理程序中调用“System.Windows.Forms.Application.Restart()”将在更新完成后为您执行此操作。
,好吧,您可能需要进一步澄清您的问题。如果您希望程序取消下载任务然后重新启动程序,
webClient.CancelAsync();
System.Diagnostics.Process.Start(Application.ExecutablePath);
System.Diagnostics.Process.GetCurrentProcess().Kill();
否则,如果您希望程序取消下载任务并重新开始下载,
webClient.CancelAsync();
//Initializing A New One So It Can't Cause Any Issues
webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync("https://195.50.139.130:5001/FTP/KlingGastro Update/file.txt","C:\\file.txt");
希望有帮助!如果没有帮助,请让我们确切地知道您要实现的目标。