Lan Yung Lee
Lan Yung Lee

Reputation: 31

Adding a character into a string after a specific character

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

Answers (3)

Lan Yung Lee
Lan Yung Lee

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

Zohar Peled
Zohar Peled

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

Sergey Kalinichenko
Sergey Kalinichenko

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:

  • Use verbatim literals - prefix it with "at" sign, i.e. @"C:\\yung\\Desktop", or
  • Double each slash - put two slashes for each slash in the result: C:\\\\yung\\\\Desktop

Upvotes: 1

Related Questions