简体   繁体   English

在Winforms中保存文件

[英]Save File in winforms

I am new to winforms. 我是winform的新手。 When I am trying to save a file in winforms with the code below it is giving me an error that says: URI formats are not supported. 当我尝试使用以下代码将文件保存在winforms中时,出现一个错误消息: URI formats are not supported.

Please tell me how I can save the file from source path to destination path. 请告诉我如何将文件从源路径保存到目标路径。 Thanks in advance. 提前致谢。 Here is my code: 这是我的代码:

 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

            }
        }

You can't use System.IO.File to copy files from URI, you must download file to temp location and copy it by using System.IO.File.Copy(fromPath, toPath); 您不能使用System.IO.File从URI复制文件,必须将文件下载到临时位置,然后使用System.IO.File.Copy(fromPath,toPath)复制它; As the error say "URI formats are not supported." 错误提示为“不支持URI格式”。 you can't copy URI. 您无法复制URI。 Code to download file from Internet: 从Internet下载文件的代码:

using (var client = new WebClient())
{
    client.DownloadFile("http://blablabla.pl/file.png", "C:\Path\To\Save\File\a.png");
}

i suggest to use it on another thread, downloading big files may freeze UI Thread! 我建议在另一个线程上使用它,下载大文件可能会冻结UI线程!

And the next bug is: BrwsBussinessTaxReturn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Text|*.txt|Office Files|*.doc;*.xls;*.ppt"; 而下一个错误是: BrwsBussinessTaxReturn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Text|*.txt|Office Files|*.doc;*.xls;*.ppt"; should be defined before BrwsBussinessTaxReturn.ShowDialog(); 应该在BrwsBussinessTaxReturn.ShowDialog();之前定义BrwsBussinessTaxReturn.ShowDialog();

Might be a bit late, but I suggest you download it with a httpclient like this: 可能会有点晚,但是我建议您使用httpclient这样下载它:

    private async void getfile()
    {
        HttpClient c = new HttpClient();
        string file = await c.GetStringAsync("http://example.com/");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM