C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 7th, 2010, 09:17 PM
scryptKiddy scryptKiddy is offline
Participant
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: Hawaii
Posts: 376 scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 10 h 32 m 6 sec
Reputation Power: 15
Send a message via Skype to scryptKiddy
C# Error: The specified path, file name, or both are too long

I have a C# Windows Form application that searches a file for a particular string. When it gets to files that are in directories that are too long it throws this error:

LOG Code:
Original - LOG Code
    The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.


Here is my function (simplified / psuedo), which called in a loop, reading a list of fully qualified file names:

C# Code:
Original - C# Code
  1. public string scanFile(string filePath)
  2.         {
  3.             try
  4.             {
  5.                 /****** ERROR IS THROWN LINE BELOW ******/
  6.                 TextReader reader = new StreamReader(filePath);
  7.                 string line = "";
  8.                 while ((line = reader.ReadLine()) != null)
  9.                 {
  10.                     .....do lots of stuff
  11.                 }
  12.                 if (yada yada)
  13.                 {
  14.                     return report;
  15.                 }
  16.                 else
  17.                 {
  18.                     return null;
  19.                 }
  20.             }
  21.             catch (Exception e)
  22.             {
  23.                 throw e;
  24.             }
  25.            
  26.         }


It is already a mapped drive, saving some characters, but it still is really long for some directories.

Ideas?

SK

Reply With Quote
  #2  
Old September 7th, 2010, 11:15 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
Can't you temporarily SUBST a large portion of the path?
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

Reply With Quote
  #3  
Old September 8th, 2010, 09:20 AM
grisson grisson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2010
Posts: 27 grisson User rank is Corporal (100 - 500 Reputation Level)grisson User rank is Corporal (100 - 500 Reputation Level)grisson User rank is Corporal (100 - 500 Reputation Level)grisson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 5 m 22 sec
Reputation Power: 0
Do you get the same problem when you use a System.IO.FileInfo object?
You can open the file stream from that object and accomplish the same thing you are trying to do.
Code:

            FileInfo fi = new FileInfo(filePath);
            TextReader tr = new StreamReader( fi.OpenRead()) as TextReader;

Reply With Quote
  #4  
Old September 8th, 2010, 01:33 PM
scryptKiddy scryptKiddy is offline
Participant
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: Hawaii
Posts: 376 scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 10 h 32 m 6 sec
Reputation Power: 15
Send a message via Skype to scryptKiddy
Thanks for the replies.

Not sure what you mean by SUBST, the path is a fixed string, eventually in the code it will have to resolve to the FQDN. Unless you have an example, I guess I don't understand what you mean, sorry.

As far as the FileInfo, I have not tried that, but I will, hopefully that works.

Thanks,

SK

Reply With Quote
  #5  
Old November 15th, 2012, 10:04 AM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 120 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 19 h 7 m 57 sec
Reputation Power: 44
The maximum length for a path in Windows used to be 260 characters. Recent versions allow paths up to about but not necessarily exactly 32,767 characters.

I'm assuming that your app is throwing a PathTooLongException. So, with that said, I'm attaching a link to a detailed MSDN discussion on this issue.

Naming paths and file names

**EDIT**

Another link that might be of interest to you.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C# Error: The specified path, file name, or both are too long

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap