简体   繁体   中英

How to make a string variable literal?

You know how you make a string literal or "escaped" with the @ sign right?

string foo = @"\\escaped\\string"

I was searching all over the internet, and I cannot find a way to make a string variable do that. If you are curious why I want to do that, here is a bit of backstory:

I was making a C# addon for my batch script, which would base64 a string and save it in a file. However, I kept getting an error by the name of "Illegal characters in path name" or something among those lines. So naturally, I went away googling and the only "fix" I could find is to @ the string. However, the problem is, I don't have just a simple plain text string variable. I get the location of the executable and then put it into a variable, so I can create the file right where the executable is located (the same folder). And, as I said previously, I cannot do that, because of weird path errors. I tried String.Replace and Regex.Escape but none of them worked, because it seems like the error is right when I call the function to get the location. For reference, this is the "command" I'm using to get the location of the executable.

System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)

And yes, I did try other tricks, like AppDomain.CurrentDomain.BaseDirectory and a bit more but none of them seem to work.

Any way to solve this? I'm getting really frustrated because this is supposed to be easy...

也许在处理字符串/路径操作时可以尝试使用静态方法Path.Combine

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