0

I am new to winforms. 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.

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

            }
        }
Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
Vishvadeep singh
  • 1,624
  • 1
  • 19
  • 31

2 Answers2

1

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); As the error say "URI formats are not supported." you can't copy URI. Code to download file from 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!

And the next bug is: BrwsBussinessTaxReturn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm|Text|*.txt|Office Files|*.doc;*.xls;*.ppt"; should be defined before BrwsBussinessTaxReturn.ShowDialog();

Enter
  • 87
  • 1
  • 9
0

Might be a bit late, but I suggest you download it with a httpclient like this:

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