简体   繁体   English

在字符串C#中将“ \\”替换为“ \\\\”

[英]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=' '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. UniqueID是一个字符串变量,用于保存引发错误的部分,因为它认为斜杠的两侧都是路径的一部分。 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. 如果这一切令人困惑,那么我需要做的是让'W8D6m \\ 2alNzPUW2d2m4V9EksLHg ='认为它是完整路径,因为它是文件名而不是文件夹。 I want this to be the name of a file. 我希望这是文件名。 An example is this http://prntscr.com/9tm9ed 例如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. 您似乎正在使用标准的Base64字母,所以我建议使用字符“-”,因为它是不用作Base64编码一部分的普通字符。

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

That character can be replaced by a ton of other characters (*._~ etc) that aren't used in regular Base64 though. 该字符可以替换为常规Base64中未使用的大量其他字符(* ._〜等)。

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. 您不应该使用*()\\ etc字符来构造文件/文件夹名称,它具有特殊含义。

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 用诸如“ string”之类的特殊字符串编写它,然后在读取时将其转换回即可

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. 请注意,仅替换反斜杠并不能保证文件名有效,因为根据Path.GetInvalidPathChars()数组,还有其他一些无效的字符。

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" . 用另一个字符串替换输入字符串中的反斜杠可能会引起冲突,例如"hello\\world".Replace('\\','-')提供的输出与"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. 请注意,您可能仍要检查路径是否过长,某些API将路径中的字符数限制为260个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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