我是winforms的新手。当我尝试用下面的代码在winforms中保存一个文件时,它给我一个错误信息:URI formats are not supported.
请告诉我如何将文件从源路径保存到目标路径。提前谢谢。下面是我的代码:
private void BtnBussinessBalanceSheet_Click(object sender, EventArgs e)
{
var sourceFile = "http://112.196.33.86:131/Documents/BussinessDocuments/";
if (BrwsBussinessTaxReturn.ShowDialog() == DialogResult.OK)
{
BrwsBussinessTaxReturn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Text|*.txt|Office Files|*.doc;*.xls;*.ppt";
File.Copy(BrwsBussinessTaxReturn.FileName, sourceFile + BrwsBussinessTaxReturn.SafeFileName); //error occured
}
}
发布于 2016-11-01 14:57:14
您不能使用System.IO.File从URI复制文件,您必须将文件下载到临时位置并使用System.IO.File.Copy(fromPath,toPath)复制它;因为错误显示"URI格式不受支持“。您不能复制URI。从Internet下载文件的代码:
using (var client = new WebClient())
{
client.DownloadFile("http://blablabla.pl/file.png", "C:\Path\To\Save\File\a.png");
}
我建议在另一个线程上使用它,下载大文件可能会冻结UI线程!
下一个错误是:应该在BrwsBussinessTaxReturn.ShowDialog();
之前定义BrwsBussinessTaxReturn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Text|*.txt|Office Files|*.doc;*.xls;*.ppt";
发布于 2019-01-23 16:57:01
可能有点晚了,但我建议您使用httpclient下载它,如下所示:
private async void getfile()
{
HttpClient c = new HttpClient();
string file = await c.GetStringAsync("http://example.com/");
}
https://stackoverflow.com/questions/39077634
复制相似问题