简体   繁体   中英

Replacing '\' with “\\” in string C#

I have a "simple" problem with string replacing.

I have an unknown amount of strings that follow a similar system to this:

'W8D6m\\2alNzPUW2d2m4V9EksLHg='

Notice the '\\' in the string, what I want to do is have the program to ignore it and not treat it as an escape character. The error is being thrown from this code

File.WriteAllText(Application.StartupPath + "\\Users\\" + UniqueID, NewUser.ToString());

The UniqueID is the string variable that holds the part that is throwing the error as it is thinking both sides of the slash are part of the path. So I am trying to replace it by doing:

UniqueID = UniqueID.Replace(@"\", "\\");

However that didn't work either, so I am trying to figure out, how can I make the program ignore if the string contains a "\\" (it needs to be there, so a simple remove can't be a solution)

If this is all confusing, what I need done is for the 'W8D6m\\2alNzPUW2d2m4V9EksLHg=' to think that it is a complete path since it is a file name, not a folder. I want this to be the name of a file. An example is this http://prntscr.com/9tm9ed

The issue lies in the fact that you're trying to store a backslash as a file name, which isn't possible. If you could do that, how would the computer know whether or not you're attempting to access a directory or a file?

Your best option is to replace the backslash with another character. You seem to be using the standard Base64 alphabet so I'd suggest the character "-" as it's a common character that isn't used as part of Base64 encoding.

uniqueID.Replace(@"\", "-");

That character can be replaced by a ton of other characters (*._~ etc) that aren't used in regular Base64 though.

Make sure to replace the character with a backslash when trying to interpret it, though!

The problem with \\ in this string is that when you concatenate:

<Startup>\Users\W8D6m\2alNzPUW2d2m4V9EksLHg=

it makes new path with following components:

<Startup> \ Users \ W8D6m \ 2alNzPUW2d2m4V9EksLHg=

You should not use *()\\ etc characters which has special meaning to construct file/folder names.

You simple can't have "\\" in the file name. Write it with a speical string like "☺", then convert it back when read it might do the trick

A solution that is guaranteed to give you a correct file name regardless of input:

static string GetValidFileName(string data) 
{
    var bytes = System.Text.Encoding.UTF8.GetBytes(@"W8D6m\2alNzPUW2d2m4V9EksLHg=");
    return Convert.ToBase64String(bytes).Replace("/", "_");
}

Note that replacing just the backslash does not guarantee a valid file name, since there are other characters that are invalid according to the Path.GetInvalidPathChars() array.

Replacing the backslash from the input string with another string may introduce collisions, eg "hello\\world".Replace('\\','-') gives you the same output as "hello-world" .

Note that you may still want to check if the path is too long, some APIs limit the number of characters in a path to 260 characters.

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