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,396
  • 3
  • 15
  • 20
vishu minhas
  • 1,588
  • 1
  • 17
  • 30
  • 1
    The errors says it all. You need a local/lan path not an URI for File.Copy – Steve Aug 22 '16 at 10:55
  • You are trying to Upload the File to a Webserver? Not copying it locally or? – Mark Aug 22 '16 at 10:56
  • @Steve thanks for your reply but is there any way to do this ? – vishu minhas Aug 22 '16 at 10:58
  • 2
    What are you trying to do is called Upload file to a remote server and cannot be done with file copy but with proper web methods https://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(v=vs.80).aspx – Steve Aug 22 '16 at 11:00
  • http://stackoverflow.com/questions/12968138/how-to-upload-a-file-in-window-forms – Svein Terje Gaup Aug 22 '16 at 11:49

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
  • 139
  • 1
  • 10