1:下载文件
代码语言:javascript复制 void runFile(SharedFile r)
{
var filepath = Path.Combine(Application.StartupPath, "Doc", r.FileName);
if (File.Exists(filepath))
{
if (r.Viewed)
{
System.Diagnostics.Process.Start(filepath);
return;
}
else
{
File.Delete(filepath);
}
}
dgSharedFile.Enabled = false;
waitingFilePanel.Visible = true;
this.Cursor = Cursors.WaitCursor;
wc = new WebClient();
wc.DownloadFileCompleted = new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
try
{
var url = string.Format("{0}files/Doc/{1}.{2}",
ServiceAgent.GetServiceUrl(),
r.Id,
r.FileName.Split('.').LastOrDefault());
var uri = new Uri(url);
wc.DownloadFileAsync(uri, filepath, r);
}
catch
{
Program.Alert("此文件正在被另一个进程所使用,请先关闭使用该文件的进程");
}
}
2.下载完成后打开
代码语言:javascript复制 void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
var r = e.UserState as SharedFile;
var filepath = Path.Combine(Application.StartupPath, "Doc", r.FileName);
dgSharedFile.Enabled = true;
waitingFilePanel.Visible = false;
this.Cursor = Cursors.Default;
if (e.Cancelled)
{
if (File.Exists(filepath))
{
File.Delete(filepath);
}
}
else
{
try
{
System.Diagnostics.Process.Start(filepath);
if (!r.Viewed)
{
ServiceAgent.ViewSharedFile(r.Id);
UpdateSharedFile();
}
}
catch
{
Program.Alert("文件打开失败");
}
}
}