Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on 

Specified path,filename or both are too long. The fully qualified file must be less than 260 characters.

Here is my load routine.
 private void btnLoad_Click(object sender, EventArgs e)
        {
            // Process Spreadsheet
            btnLoad.Enabled = false;

            if (dr["LOAD_DTM"].ToString() != null)
            {
                clsDeletePriceListDetails.DeletePriceListDetails(dr["PRICE_LIST_HDR_ID"].ToString());
            }
            if (InputFileName.Text != "")
            {
                // Check if files exist
                if (File.Exists(InputFileName.Text))
                {
                    lblMsg.Text = "Load Started.";
                    Application.DoEvents();
                    System.IO.FileInfo filepath = new System.IO.FileInfo(InputFileName.Text);
                    clsGlobal.Global.Path = filepath.DirectoryName;
                    ProcessSpreadSheet();
                   
                    btnGenerate.Enabled = true;
                    grpboxGenerateOttawaFile.Enabled = true;
                    btnLoad.Enabled = true;
                }
                else
                {
                    lblMsg.Text = "File does not exist. Please select a spreadsheet file.";
                    return;
                }
                lblMsg.Text = "Load Completed.";
                   
            }
            else
            {
                lblMsg.Text = "Please select a spreadsheet file.";
                return;
            }
            btnGenerate.Enabled = true;
            grpboxGenerateOttawaFile.Enabled = true;

        }

and here is my browse button routine

       private void btnFindFile_Click(object sender, EventArgs e)
        {

            OpenFileDialog getInputFile = new OpenFileDialog();

            getInputFile.InitialDirectory = "c:\\";
            getInputFile.Filter = "Excel files (*.xls)|*.xls";
            getInputFile.FilterIndex = 2;
            getInputFile.RestoreDirectory = true;

            if (getInputFile.ShowDialog() == DialogResult.OK)
            {
                InputFileName.Text = getInputFile.FileName;
                OutputFolder.Text = Path.GetDirectoryName(InputFileName.Text);
                clsGlobal.Global.Path = OutputFolder.Text;

                // update datarow global var.
                dr["SRC_FILE_PATH_DESC"] = InputFileName.Text;
                btnLoad.Enabled = true;
                btnCompare.Enabled = false;
            }
        }
.NET ProgrammingC#

Avatar of undefined
Last Comment
abel
Avatar of abel
abel
Flag of Netherlands image

So, I assume you get an error. But when and where? Is it on a Getter? Because it is impossible to have a pathname longer then 260 characters on a Windows machine, is the path you retrieve really longer? Does it exist?

Or is it from user entry, in which case the error is correct: windows does not permit longer filenames (normally it is safer to stay below about 240 character to have some slack).
So, you really have a path/file name so long? none of the .net or WinAPI will accept you such large filenames.
Avatar of mathieu_cupryk

ASKER

Is there a way to check this and then update them with a message.
Avatar of abel
abel
Flag of Netherlands image

Yes, of course. You can check the filename, when entered by the user, for its length, and, possibly propose a truncated version.

Btw: it is not only windows or the methods of windows not accepting filenames that long, but it is a restriction of the underlying filesystem driver of NTFS. And if you use FAT or FAT32 the maximum filename length is even shorter.

In practice these things only happen with deep paths inside ZIP or RAR archives that you extract in a path that's already quite deep (for instance a user's folder). The way to work around it then is to extract the files into a directory that is very short, like C:\TMP. Maybe that would work for you too.
Avatar of mathieu_cupryk

ASKER

In the above code where should I put the max length validation.
Avatar of abel
abel
Flag of Netherlands image

Well, remember my first comment? I asked about where / when the error occurred. You haven't answered that yet, so I am guessing here.

You use a filename entered by the user on two occasions in the code above. The first is as a result of the browse button. It is unlikely that something occurs there since the file comes from your system. But it is returned in the edit box, which is perhaps editable, hence a user may alter that file to become longer....

Then you have a second place where you use the filename, which is around File.Exists. The method File.Exists would return False if the path were too long. Though this would be the preferred place for checking the length of the path (InputFilename.Text.Length < 260) it is unlikely that the error occurs here.

So I am tempted to believe that you did not show everything of your code yet and more importantly, not the part where the error comes from.

Can you update and tell us the exact error including line number? And can you show the line(s) where it goes wrong?

Regards,
-- Abel --
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of abel
abel
Flag of Netherlands image

Tx & Glad I could be of some help ;-)
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo