-1

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?

Rikus Honey
  • 534
  • 1
  • 3
  • 17
  • The maximum path length is actually 260 characters, including the file name. See [this](http://stackoverflow.com/questions/1880321/why-does-the-260-character-path-length-limit-exist-in-windows) answer and related MSDN link. – codechurn Feb 20 '16 at 14:26
  • 1
    Possible duplicate of [Best way to resolve file path too long exception](http://stackoverflow.com/questions/8745215/best-way-to-resolve-file-path-too-long-exception) – yaakov Feb 20 '16 at 14:26
  • 2
    "the result is far below 508 (the fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters)." - The 260 length limit includes the 248 for the directory, you don't get to add the two together. – yaakov Feb 20 '16 at 14:27
  • @codran thanks for that, but the legnth is still lower than 260 and the exception get thrown! – Rikus Honey Feb 20 '16 at 14:29
  • 1
    So Windows says your path is too long. Windows has seen your path. You say your path is not too long. You have seen your path. But you're asking why Windows is saying it's too long to people who *haven't* seen your path. What sort of response are you hoping for other than "without seeing your path, I'm guessing you're counting wrong"? –  Feb 20 '16 at 14:35
  • @hvd I apologise, here is the example I tried: Let's say the user enters "test.t6mp_dem" under the Desktop folder (C:\Users\Username\Desktop\), which is 39 chars long + termination character = 40, but the exception still occurs... – Rikus Honey Feb 20 '16 at 14:36
  • Please cleanup your sample so it contains all values necessary to reproduce problem inline in the code. See [MCVE] for guidance. In current state there is no way to figure out what problem you have (and there is no stack trace in the question making it even harder to diagnose). – Alexei Levenkov Feb 21 '16 at 03:18

2 Answers2

1

The problem never was the filename, but rather the address from where the filename was downloading. I fixed the function that returned the server address to download from and everything works now.

Rikus Honey
  • 534
  • 1
  • 3
  • 17
0

It is normal cause maximum path is 256 character string. See this some information that should help you solve the problem:

Maximum Path Length Limitation In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Let's say the user enters "test.t6mp_dem" under the Desktop folder (C:\Users\Username\Desktop), which is 38 chars long + termination character = 39, but the exception still occurs... – Rikus Honey Feb 20 '16 at 14:36