Search code examples
c#remote-server

c# The given path's format is not supported. UNC Path


I am trying to access and download a bak file from a remote server and keep getting the error "The given path's format is not supported." The code I am using below:

string uncPath = Server.MapPath(Path.Combine(@"\\TSTSVR\Users\temp_databaseBackups_000kfkf000", 
  string.Format("{0}-{1}.bak", ddlDatabases.SelectedValue, DateTime.Now.ToString("yyyy-MM-dd"))));

//download
WebClient webClient = new WebClient();
webClient.DownloadFile(uncPath, ddlDatabases.SelectedValue + "-" + DateTime.Now.ToString("MM-dd-yyyy:hh:mm"));

I am getting the error at the DownloadFile line. Am I declaring the UNC path wrong? The folder is there on the server and I set access to Everyone with write permissions.


Solution

  • With the help of a few hints in the right direction from you all I solved the problem with the following code:

    string uncPath = Path.Combine(@"\\TSTSVR\Users\temp_databaseBackups_000kfkf000",
      string.Format("{0}-{1}.bak", ddlDatabases.SelectedValue, DateTime.Now.ToString("yyyy-MM-dd")));
    
    //download
    Response.ContentType = "bak";
    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + ddlDatabases.SelectedValue + "-" + DateTime.Now.ToString("MM-dd-yyyy:hh:mm") + "\"");
    
    Response.TransmitFile(uncPath);
    Response.End();
    

    The program will now download the file need from the remote server.