Reputation: 31
In order to be able to read a file in asp.net, the file path must be written in this: 1.
C:\\yung\\Desktop
returns however, the string that the fileUpload get returns is 2.
C:\yung\Desktop
After reading the comments i have this code:
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
string line = File.ReadLines(FilePath.ToString()).Skip(4).ToString();
TextBox1.Text = line.ToString();
But now its giving this error:
System.Linq.Enumerable+<SkipIterator>d__30`1[System.String]
How to solve this problem?
Thank you.
Upvotes: 0
Views: 481
Reputation: 31
Ok, i have manage to solve this problem, turns out it was not reading anything.
This is the code that i finally get:
This is to retrieve the File's path, using this, would give the file path will double slash, so there is not a need for Replace(@"\",@"\")
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
Then read the specified file
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
If you know which line you specifically want, this retrieves the 5th line
string line = File.ReadLines(FilePath.ToString()).Skip(4).First().ToString();
Thank you so much for your help...
Upvotes: 1
Reputation: 82474
I'm not so sure I understand the question, but I think you are looking for string.Replace
:
string DoubleSlash(string singleSlash)
{
return singleSlash.Replace(@"\", @"\\");
}
Upvotes: 2
Reputation: 726509
The reason backslashes disappear is that C# compiler treats slashes in string literals as a special "escape" character. Because of this treatment, backslash needs to be encoded as two slashes in a regular string literal.
C# offers two ways of inserting backslashes the way you need:
@"C:\\yung\\Desktop"
, orC:\\\\yung\\\\Desktop
Upvotes: 1