I am trying to download a file to a specified path, but when called, then I get a PathTooLongException.
public async void SaveDemo(string filename)
{
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadcompleted);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadchanged);
await wc.DownloadFileTaskAsync(Functions.GetLink(), filename);
}
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "T6MP Demo File|*.t6_dem";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
textBox1.Text += Environment.NewLine + "Downloading demo...";
//textBox1.Text += Environment.NewLine + sfd.FileName + Environment.NewLine + sfd.FileName.Length;
SaveDemo(sfd.FileName);
}
catch (Exception ex)
{
textBox1.Text += ex.ToString();
throw;
}
}
else
{
return;
}
}
I've tried debugging to display the actual legnth of the string entered by removing the commented line and the result is far below 260 (the fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters). Why does the exception occur?
EDIT
I now even hardcoded a filename, "C:\testc.t6mp_dem", to the function so it now looks like this:
SaveDemo("C:\\testc.t6mp_dem");
but the PathTooLongException still occurs, which can't be caused by the filename being too long. So why does this exception still occur?