C# automatically substitutes forward-slash with back-slash in path...why? - c#

I have a textbox where I put the name of a file that contains the forward-slash character '/'
When I grab the text from the textbox the '/' is automatically replaced with '\' and it obviously can't find the file, for example:
I write C:\Users\Temp\my/file.txt
I get C:\Users\Temp\my\file.txt
So, instead of opening "my/file.txt", it searches for a directory "my" which contains "file.txt"
How can I solve this?

If you're using Windows (which I assume you are, given the paths in your example), then / is an invalid character for a filename:
The easiest solution here would be to remove the slash from the filename as this is likely to cause further issues down the line.

Related

C# Save file with "/" in name

FileName contains e.g. Legend/Dery//Times
File.WriteAllBytes("/Pictures" + FileName, buffer);
I can´t save the file because the "/" considered as path, I also can´t remove the "/", because I need it for further processing. Is there any way of saving such file?
You're out of luck. A forward slash can't be part of a file name.
You need to escape it somehow (i.e. change the name but provide a way of changing it back), but there isn't really a conventional way of doing that.
I've seen % been used for this purpose, with %% used to denote a single %, and something like %f for a forward slash, %b for a backslash, etc.
There are rules for names and folders defined by Microsoft that mean you are not allowed to do this.
Instead of escaping in i suggest normalizing your input both when you save a file and when you try to access a file:
//replace all illegal characters with regex (with a dash):
new Regex(#"[<>:""/\\|?*]").Replace("Inpu|t","-")
//Or just replace all non alpha numeric characters (with a dash):
new Regex(#"[^a-zA-Z0-9\-]").Replace("Inpu|t","-")
this way you will always have clean file and folder names and don't have to worry about illegal names.

Using The # symbol for a Directory so two backslashes aren't needed

this seems very simple but upon research I cannot find out how to use the # sign in a directory to prevent it from having to be to backslashes.
An example is of
DirectoryInfo folderInfo = new DirectoryInfo(#"C:\");
But In my Application The directory will be dynamic so I cannot do this:
DirectoryInfo folderInfo = new DirectoryInfo(#Globals.directoryRoute);
So I was wondering what is the correct way to put the # symbol before the string.
Globals.directoryRoute is set as C:\ but the user can change this input so I was hoping instead of having to parse out every double backslash I can use this to make it so only one backslash is needed.
Would this be an effective way of doing it or should I just parse out every second backslash?
The # prefix is a tool to tell the compiler to not take the backslash as an escape character within the following string. If the string is entered at runtime, you don't need to worry about that. So you can just use the content of Globals.directoryRoute as it is.
The double backslashes are only needed for string literals in your code. In memory, only a single backslash is stored in the string, so no # symbol is needed when dealing with strings that are already in memory. Similarly, user input does not need the double backslashes, since it is not interpreted in the same manner as source code. For instance, if you have a text box called txtPath, the user can simply type C:\some\path, not C:\\some\\path as you would normally need to do in source code. When you read the value of that text box in code, you can just use:
string path = txtPath.Text;
This will be the same as if you had the following code:
string path = #"C:\some\path";
or, equivalently:
string path = "C:\\some\\path";

System.ArgumentException: Illegal characters in path. error

I`m getting an ArgumentException from the following code:
string strPath="C:\somename.xls";
startPath=System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
filePath = System.IO.Path.Combine(startPath, strPath);
I found this code on Stack Overflow.
Link:
C#:Copy protected worksheet to another excel file
I don't exactly know what it is. Please tell me what it is. This code I'm building into an exe.
Lastly, I need to Copy one worksheet to another file.
What`s wrong am I doing? I deploy this in server.
what that code appears to do, is it gets your working directory(wherever the exe associated with your code is), and combines it with "C:\\somename.xls"(which doesn't make sense.)
I think you might have intended something like
string strPath=#"somename.xls";
so assume you're running your application from
"C:\Users\owner\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug"
what that code would do is set filePath to
"C:\Users\owner\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\somename.xls"
the first thing I saw was
string filePath="C:\somename.xls";
\ is a special character, for determining other characters. for instance '\n' is a newline. '\\' is the actual backslash.
so, you want to escape your \ with another \
string filePath="C:\\somename.xls";
or make it a literal string by putting a # in front of it.
string filePath=#"C:\somename.xls";
Your code should be:
string filePath = "C:\\somename.xls"
You need double backslashes.
Two problems with the code,
First
string filePath="C:\somename.xls";
\ is a special character, for determining other characters. for instance '\n' is a newline. '\\' is the actual backslash.
Second
filePath contains a root path, C:\\. Path.Combine will just return filePath then, it cannot be combined.
Your main problem is in startPath parameter.
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
If you trace your code, in FileName you will see a bad symbol character witch is illegal

C# Special Characters in String Crashing Program

I have a slight problem with a path:
"D:\\Music\\DJ Ti%C3%ABsto\\Tiesto\\Adagio For Strings (Spirit of London).mp3"
"D:\\Music\\Dj Tiësto\\Tiesto\\Adagio For Strings (Spirit of London).mp3"
Currently, when it sends that path to my Audio Library, it cannot open the path. (the reason for it crashing is trying to assign a -1 to a trackbar...but it's irrelevant).
So I'm wondering, is there anyway to prevent C# from switching special characters with %[code]? I've done a .Replace for "[" and "]", but I rather not have to look up every single special character, and add a line of code to prevent it. Is there anyway around this?
Call Uri.UnescapeDataString.
By the way, when putting paths in strings, you can put an # sign before the string to tell the compiler not to process escape codes, like this: #"D:\Music\DJ Tiësto\Tiesto\Adagio For Strings (Spirit of London).mp3". This way, you don't need to double up every backslash.

How to return a verbatim string from ConfigurationManager.AppSetting["settingname"].ToString()

I am using the ConfigurationManager.AppSetting["blah"].ToString() method to get the path to the folder that contains the files I'm needing. But I'm throwing an UnsupportedFormatException on the path when it tries to use Directory.GetFiles(path).
The returning value has the escape characters included and I'm not sure how to keep it from returning the extra characters. This is what the path looks like after it is returned:
\\\\\\\\C:\\\\folder1\\\\folder2
I needed to remove the first four "\" to give it a correct path.
you have extra back-slash \ at the beginning of your path.
try putting "C:\folder1\folder2" instead of "\\C:\folder1\folder2" in your config file, and it will work.

Categories