Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string text = System.IO.File.ReadAllText(@"D:\Selected Instances\Insertion graphs\1-FullIns_3.col");

this is the way to read a file what if we have file path stored on FileName variable.

What I have tried:

C#
string text = System.IO.File.ReadAllText(@FileName);

not working
Posted
Updated 14-May-16 19:19pm
v3
Comments
Garth J Lancaster 15-May-16 0:04am    
'not working' doesnt mean much to us out here - since we cant see your screen, debugger, or read your mind. Frankly, I'd make sure your file exists before trying to read it - this string text = System.IO.File.ReadAllText(@"D:\Selected Instances\Insertion graphs\1-FullIns_3.col"); is fine if D:\Selected Instances\Insertion graphs\1-FullIns_3.col exists - text should then be a string with all the data from the file in it - you can split it or whatever ....
Sergey Alexandrovich Kryukov 15-May-16 0:19am    
To start with, there are no situation where the hard-coded file path can be useful, unless this is done just for experiment/test...
—SA

I'd think about

C#
string filename = @"D:\Selected Instances...";
if (!File.Exists(filename))
{
  Console.WriteLine("File {0} Doesnt Exist", filename);
  Environment.Exit(-1);
}

string text = File.ReadAllText(filename);

// Do something with text 
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-16 0:23am    
Better, a 5. :-)
But more importantly, there are no situation where the hard-coded file path can be useful, unless this is done just for experiment/test...
—SA
Have you tried
C#
string fileName = @"D:\Selected Instances\Insertion graphs\1-FullIns_3.col";
string text = System.IO.File.ReadAllText(fileName);


The @ character cannot be used with a variable, only literal strings.
See String Class (System)[^]

Quote:
Note that in C#, because the backslash (\) is an escape character, literal backslashes in a string must be escaped or the entire string must be @-quoted.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-May-16 0:22am    
5ed. I was about to note that "The @ character cannot be used with a variable, only literal strings" sounds extremely weird, but later noticed that the weirdness is in the inquirer's sample. It's hard to explain the possible logic behind it. :-)

But more important, there are no situation where the hard-coded file path can be useful, unless this is done just for experiment/test...

—SA
George Jonsson 15-May-16 0:28am    
Thanks Sergey.
Totally agree about the hard coded paths, but what to do in a short example code like this?
And for the weirdness of my comment about @, my only defense is that I'm not a native English speaker. :P
Garth J Lancaster 15-May-16 0:42am    
your english is undoubtedly better than my Swedish (I know a few words) even less ?Ukrainian for Sergey. You're 'spot on' about the filename George - given no other input from the poster, its as much as one can do to promote a little 'sanity' ...

'skal' :-) (dont know how to get the accent via keyboard on a Mac)
George Jonsson 15-May-16 1:13am    
Tack Garth.
I'm not a Mac user, but on Windows you can use the key combination Alt+390 to produce an 'å'. No idea what the equivalent is on a Mac.
Then it becomes 'skål'.
(A bit too early yet here in the Philippines for a beer, though)
Sergey Alexandrovich Kryukov 15-May-16 1:42am    
Not Ukrainian, Russian. Who told you that. By the way, I started to understand a little more of Ukrainian after I started to watch some of Ukrainian TV via Youtube, which used to be very interesting at that time. Many understand it better then I am; it's a matter of some not very hard practice...

Our comparisons of knowledge of English is pretty much irrelevant... (I have no idea what George's native language might be, it's interesting...)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900