Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read a txt file in string from the given path contained in string.
I am getting the path but the file is not getting read properly.

After third statement i am getting error: "The given path's format is not supported."

Here is my Code in C#:

C#
string filename = "basic.htm";
string path = AppDomain.CurrentDomain.BaseDirectory + filename;
string strArray = File.ReadAllText(path);
Posted
Updated 2-Jun-15 22:06pm
v2
Comments
Tomas Takac 3-Jun-15 4:09am    
Did you check what's actually in "path" variable?
[no name] 3-Jun-15 4:39am    
In path Variable i am getting C:\Users\FolderName\filename

but when i give Hardcoded path like this C:\\Users\\FolderName\\filename
or @"C:\Users\FolderName\filename" it works.
Tomas Takac 3-Jun-15 4:44am    
So you are saying that the hardcoded string is exactly the same as the one you have in path but for some reason it doesn't work? Any special characters in path then?
[no name] 3-Jun-15 4:56am    
No Sir the string in path variable contains single \(backslash) while for running
properly we need \\
For Example : Currently i am getting path = C:\Users\FolderName\filename
but i need this C:\\Users\\FolderName\\filename
or @"C:\Users\FolderName\filename".
Hope i t will work den.
Andy Lanng 3-Jun-15 4:19am    
Agreed - check the "path" variable. You will see the issue immediately and know how to fix it ^_^

Quote:
string path = AppDomain.CurrentDomain.BaseDirectory + filename;

Should (probably) be:
string path = AppDomain.CurrentDomain.BaseDirectory + "\\" + filename;
 
Share this answer
 
Comments
Tomas Takac 3-Jun-15 4:41am    
Yes, but OP should have realized that on his own. Moreover I would suggest Path.Combine()[^] just to be safe.
[no name] 3-Jun-15 4:44am    
@CPallini : I tried your solution but didnt work..
CPallini 3-Jun-15 4:51am    
What is the error?
As suggested, use the debugger to see, at runtime, the value of the path variable, and make use it corresponds to the one of your file.
[no name] 3-Jun-15 4:59am    
The value in path variable i am getting is "C:\Users\FolderName\filename".
but i need "C:\\Users\\FolderName\\filename" or @"C:\Users\FolderName\filename"
CPallini 3-Jun-15 5:05am    
Nope, that's correct for the runtime string. Just make sure the file is there.
 
Share this answer
 
Comments
[no name] 3-Jun-15 5:16am    
Hey Tomas takac ... Got the issue solved thanks brother for helping... der was a file issue...

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