简体   繁体   中英

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.

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(@"\", @"\\");
}

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, ie @"C:\\\\yung\\\\Desktop" , or
  • Double each slash - put two slashes for each slash in the result: C:\\\\\\\\yung\\\\\\\\Desktop

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM