简体   繁体   English

在C#中转义目录字符

[英]Escaping directory chars in C#

I need to escape chars in a string I have, which content is C:\\\\blablabla\\blabla\\\\bla\\\\SQL.exe to C:\\blablabla\\blabla\\bla\\SQL.exe so I could throw a process based on this SQL.exe file. 我需要将字符串中的字符转义为C:\\\\blablabla\\blabla\\\\bla\\\\SQL.exeC:\\blablabla\\blabla\\bla\\SQL.exe这样我才能基于此SQL.exe文件。

I tried with Mystring.Replace("\\\\", @"\\"); 我尝试了Mystring.Replace("\\\\", @"\\"); and Mystring.Replace(@"\\\\", @"\\"); Mystring.Replace(@"\\\\", @"\\"); but none worked. 但没有一个成功。

How could I do this? 我该怎么办?

Thanks a lot in advance. 非常感谢。

EDITED: Corrected type in string. 编辑:更正了字符串类型。

I very strongly suspect that you are looking this input string in the Visual Studio debugger and fooling yourself that there are actually 2 \\ whereas in reality there aren't. 我非常怀疑您在Visual Studio调试器中查找此输入字符串,并自欺欺人地实际上有2 \\而实际上却没有。 That's the reason why attempting to replace \\\\ with \\ doesn't do anything because in the original string there is no occurrence of \\\\ . 这就是为什么尝试用\\代替\\\\不会执行任何操作的原因,因为在原始字符串中没有出现\\\\ And since you are looking the output once again in the debugger, you are once again fooling yourself that there are 2 \\ . 并且由于您再次在调试器中查找输出,因此您再次自欺欺人地知道有2 \\

Visual Studio debugger has this tendency to escape strings. Visual Studio调试器具有转义字符串的趋势。 Log it to a file or print to the console and you will see that there is a single \\ in your input string and you don't need to replace anything. 将其记录到文件或打印到控制台,您将看到输入字符串中有一个\\ ,并且无需替换任何内容。

It looks like you're trying to replace double backslash ( @"\\\\" ) in a string with single backslash ( @"\\" ). 看起来您正在尝试用单反斜杠( @"\\" )替换字符串中的双反斜杠( @"\\\\" @"\\" )。 If so try the following 如果是这样,请尝试以下操作

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

Note: Are you sure that the string even contains double backslashes? 注意:您确定字符串甚至包含双反斜杠吗? Certain environments will print out a single backslash as a double (debugger for example). 某些环境会将单个反斜杠打印为双反斜杠(例如,调试器)。 Your comment mentioned my approach didn't work. 您的评论提到我的方法行不通。 That's a flag that there's not actually a double backslash in your string (else it would work). 这是一个标志,表明字符串中实际上没有双反斜杠(否则它将起作用)。

The @ character specifies a string as a verbatim literal string, but that is when constructing a string. @字符将字符串指定为原义文字字符串,但这是在构造字符串时。 If you use Mystring.Replace("\\\\", @"\\") then nothing will be replaced, essentially, as the two strings are the same. 如果使用Mystring.Replace("\\\\", @"\\")则基本上不会替换任何内容,因为两个字符串相同。

If you want a string without the escape characters, then either define it with: 如果您想要一个不带转义字符的字符串,请使用以下命令进行定义:

string path = @"C:\Some\Directory\And\File.txt";

Or you can replace the \\\\ with / like so: 或者您可以将\\\\替换为/如下所示:

path = path.Replace('\\', '/');

It is worth noting, as mentioned by Darin Dimitrov, that the string containing two \\ characters is likely just the display of the string (ie when using the debugger) and not the actual value of the string. 正如Darin Dimitrov所提到的那样,值得注意的是,包含两个\\字符的字符串很可能只是字符串的显示(即,使用调试器时),而不是字符串的实际值。

i think OP is asking how to escape \\\\ in File Path, if that in the case, as OP is not mentioning where he's trying to use this. 我认为OP正在询问如何在文件路径中转义\\\\(如果是这种情况),因为OP并未提及他在尝试使用此位置。 so i'm putting a guess. 所以我猜。

Then You use Path.Combine() method to get the FileName path. 然后,您使用Path.Combine()方法获取FileName路径。

Path.Combine() Documentation Path.Combine()文档

where are you looking at this output? 您在哪里看这个输出? because it could be the string is what you expect, but viewing the value through the debugger, output window, etc. is escaping the slash 因为它可能是您期望的字符串,但是通过调试器,输出窗口等查看值可以避免使用斜杠

Use something like: 使用类似:

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

Make sure you assign the result of Replace method to myStr. 确保将Replace方法的结果分配给myStr。 Otherwise it goes into void ;) 否则它就无效了;)

Try adding "|DataDirectory|\\MyFile.xyz" where you need it. 尝试在需要的地方添加“ | DataDirectory | \\ MyFile.xyz”。 It works with connection strings it might work with something else (I haven't really tried to apply it to something else). 它可以与连接字符串一起使用,也可以与其他东西一起使用(我还没有真正尝试将其应用于其他东西)。

我不明白您想要什么,如果您只想获取文件名(转义目录字符),则可以尝试:

string fileName = Path.GetFileName(YourString)

Noloman.... when you concatenate are you perhaps missing a "\\" when concatenating the directory.. I am assuming that you are trying to join directory + some sub directory.. @noloman keep in mind that in C# "c:\\Temp" is written like this "c:\\Temp" or @"c:\\Temp" one is Literal the other is how to represent a "\\" in the legacy way of coding because the "\\" is an escape Char and when dealing with directorys we represent all paths and sub paths with "\\" so perhaps by you replacing the "\\" you are truly messing up your own expected process Noloman ....当连接时,目录连接时可能会丢失“ \\”。.我假设您正在尝试加入目录+一些子目录。.@noloman请记住,在C#中,“ c:\\ Temp”的写法是这样的“ c:\\ Temp”或@“ c:\\ Temp”,一个是立即数,另一个是如何用传统编码方式表示“ \\”,因为“ \\”是转义字符,当在处理目录时,我们用“ \\”表示所有路径和子路径,因此也许通过替换“ \\”,您确实在弄乱自己的预期过程

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

should work for you unless you are truly meaning to do Mystring = Mystring.Replace(@"\\", "\\"); 应该为您工作,除非您确实有意做Mystring = Mystring.Replace(@“ \\”,“ \\”); which if you believe that you are expecting a "\\" to be used to build the directory.. then of course it will not work.. because you have just in essense replaced the backslash with a return char.. I hope that this makes sense to you.. 如果您认为您希望使用“ \\”来建立目录,那么它当然将不起作用..因为您只是在本质上用返回字符替换了反斜杠。.我希望这会使对你有意义。

System.IO.Directory.GetCurrentDirectory(); System.IO.Directory.GetCurrentDirectory(); you are using is also an Issue.. SQL Server is not that application thats running the code.. it's your .NET application so you need to either put the location of the SQL Server into a variable, app.config, web.config ect... please edit your question and paste the code that you are using to do what it is that you want to do inregards to the SQL Server Code.. you would probably want to look at the Are you wanting to do something like Process.Start(....) meaning the file name..? 您正在使用的也是一个问题。SQL Server不是运行该代码的应用程序。它是您的.NET应用程序,因此您需要将SQL Server的位置放入变量app.config,web.config等中。 ...请编辑您的问题,然后粘贴要用于执行与SQL Server代码无关的操作的代码。您可能希望查看“是否要执行类似Process的操作”。 Start(....)表示文件名..?

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

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