How to make a multiple files with 1 path? - c#

How to make a variable in a path in c#? For like creating a user register/login/stats.
string UserName = "";
string path = #"c:\File\File\" + UserName + ".text";
I know this doesn't work, maybe does anybody know how to do it else, I search around and never found a solution to get a path like this.
I hope somebody will solve it!

You can use / (slashes) instead of \ (backslashes) or you can escape the backslash adding another backslash behind it:
string path = "c:\\File\\File\\"+ Username + ".text";

That way is absolutly OK for simple concating strings. There are other ways like
string.Format function
or the
StringBuilder class
This is all absolutly OK but if you will be absolutly sure that you create a vaild Path use
Path.Combine

The only reason that doesn't work is because of the escape characters. Any of the following will work;
string path = "c:\\File\\File\\"+ Username + ".txt"; // escape first slash, second appears in string
string path = #"c:\File\File\"+ Username + ".text"; // take literal string, escape sequences included
string path = "c:/File/File/"+ Username + ".text"; //forward slash is not an escape

You can easily get an array of invalid file name characters
char[] invalidPathChars = Path.GetInvalidPathChars();
foreach (char ch in invalidPathChars)
{
Username = Username.Replace(ch.ToString(), "");
}

Related

Add file extension to variable

I am new to c sharp
can anybody say what the mistake
string cPict= "Picture\"+firstSelectedItem+".jpg";
where
"Picture\" = folder
firstSelectedItem = Employee Number
".jpg" = file extension
getting following error
string does not contain definition for jpg
please help
thanks in advance
The problem is that in "\"+firstSelectedItem all is treated as string, even the firstSelectedItem variable because you've used the \-character to escape the following ".
You either have to
escape the \-character by another one,
use a verbatim string literal or
use the Path-class, especially Path.Combine:
1)
string cPict = "Picture\\" + firstSelectedItem + ".jpg";
2)
string cPict = #"Picture\" + firstSelectedItem + ".jpg";
3)
string cPict = Path.Combine("Picture", firstSelectedItem + ".jpg");
You can replace it with normal slash like that:
string cPict= "Picture/"+firstSelectedItem+".jpg";
The \ is a special character that escapes the next character in a string, therefore, according to the compiler then + firstSelectedItem + is still part of the string. Your code should look like one of the following:
string cPict = #"Picture\" +
or:
string cPict = "Picture\\" +
and that should work.
you need to escape the backslash \ character
string cPict= "Picture\\"+firstSelectedItem+".jpg";
learn about Escape Sequences here
The solution is to add double slashes as below:
string cPict= "Picture\\"+firstSelectedItem+".jpg";
"Picture\\"=folder

How to use " in a String with a path?

I have a String with a path in it.
It looks like this :
TaskManager = "RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");"
I want to use this string to compile with codedom, but I get an error saying "cant find Software\Microsoft......".
Is there anyway to replace the " with another char?
I use verbatim string literal for paths so I don't have to double the backslashes:
TaskManager = #"RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey(""Software\Microsoft\Windows\CurrentVersion\Policies\System"");"
" is doubled ""
to write " inside a string use \"
String str= "my string with \"quotes\""; // my string with "quotes"
the character \ is used to write special character inside a string for example a \t write inside the string a tab and \n make the string go to a new line
or
String str= #"my string with ""quotes"""; //same as before
the character # before a string make the compiler take the string as is, every special character is written without having to use \ before it, the only character that need to be escaped are the quotes itself that you write by doubling them ""
Watch out that iff you use the method with # your \ \ in the path become \
If you are trying to escape the qoute then do something like this:
TaskManager = "RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey(\"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\");"
Use \"
to get a double quote, the same way as you used \ to get a backslash.
TaskManager = "RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey(\"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\");"
That should fix it.
you can use it this way
TaskManager = "RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey('Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System');"

Changing "/" to "\" [C#]

I have already seen the other way around. But this one I can not catch. I am trying to get a part of a web resourcePath and combine it with a local path.
Let me Explain a bit more.
public string GetLocalPath(string URI, string webResourcePath, string folderWatchPath) // get the folderwatcher path to work in the local folder
{
string changedPath = webResourcePath.Replace(URI, "");
string localPathTemp = folderWatchPath + changedPath;
string localPath = localPathTemp.Replace(#"/",#"\");
return localPath;
}
But, When I do this the result is like
C:\\Users
But what I want to have is
C:\Users
Not "\\" but my debug shows it like C:\\Users but in the console it shows it as I expect it.
I want to know the reason for that
thanks..
Because \\ is escape sequence for \
string str = "C:\\Users";
is same as
string str = #"C:\Users";
Later one is known as Verbatim string literal.
For combining paths in code it is better to use Path.Combine instead of manually adding "/"
Your code should be like
public string GetLocalPath(string URI, string webResourcePath,
string folderWatchPath)
{
return Path.Combine(folderWatchPath, webResourcePath.Replace(URI, ""));
}
There is no need to replace / with \ because path names in windows supports both. So C:\Users is same as C:/Users
In C#, \ is special in ""-delimited strings. In order to get a literal \ in a string, you double it. \ is not special in #"" strings, so #"\" and "\\", or #"C:\Users" and "C:\\Users" mean exactly the same thing. The debugger apparently uses the second style in your case.
I believe that debug shows strings with escape chars, and to escape a \ in a non-verbatim (not prefixed with #) string you have to write \\.

How to quote \" (slash double-quote) in a string literal?

This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \" without it seeing it as an escape character. I tried using # but it won't work. The only other way I thought of doing this would be to use \u0022 but don't want to unless I can help it.
Desired string - string s = "\"\""; // Obviously this doesn't work!
Desired console output - \"\"
Hope this makes sense.
Thanks!
Try
string s = "\\\"\\\"";
You have to escape your backslashes too.
Mike
You can use the literal, but you need to double-up quotes.
string s = #"\""\""";
In verbatim string literals (#"...") a " in the string value is encoded as "", which happens to also be the only escape sequence in verbatim strings.
#"\""Happy coding!\""" // => \"Happy coding!\"
"\\\"Happy coding!\\\"" // => \"Happy coding!\"
Note that in the 2nd case (not a verbatim string literal), a \ is required before the \ and the " to escape them and prevent their normal meanings.
See the C# string reference for more details and examples.
I think you have to escape backslashes too... so something like "\\\"\\\"" should work, I believe.
Use this string:
string s = "\\\"\\\"";
Console.WriteLine( "\\\"\\\"" );
Just put a \ before each character that needs to be printed.
String s = #"\""\""";
DblQuote characters will escape a second dblquote character
Though for better readability I would go with:
const String DOUBLEQUOTE = """";
const String BACKSLASH = #"\";
String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;
In a verbatim string (a string starting with #"") to escape double quotes you use double quotes, e.g. #"Please press ""Ok"".". If you want to do it with verbatim strings then you would do something like #"\""" (that's 3 double quotes on the end there).
You can do like this,
string s = "something'\\\'";
Use a single '' rather then "" in string to do the same.

How can I ignore escape character Vertical tab

I have a issue when I look up a user name using the code below if the user has \v in their name. For Example, if their user name is xxx\vtest, it can't find the backslash when using the LastIndexOf function.
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
If I do try this, it works with no problems.
string strUser = #"xxx\vtest";
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
Any ideas on how to ignore the escape character while using User.Identity.Name?
If the name has 'backslash v' you should find a backslash, if the name has a vertical tab (escaped '\v') you will not find a backslash. The reason you find a backslash in the second version is because you used an # with the string declaration more info.
try this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\v") + 1);
EDITED
If you know which escape characters you are looking for you can also use this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOfAny(new char[]{'\b','\t','\n','\v','\f','\r','\\'}) + 1);
If you know that there is exactly one character that follows the domain you can also use Regex to get the user.
string strUser = this.Context.User.Identity.Name;
strUser = Regex.Replace(strUser, #"^(xxx).(.+)$", "$2");
// or
strUser = Regex.Replace(strUser, #"^([a-zA-Z]*?)[^a-zA-Z](.+)$", "$2");
// or
strUser = Regex.Replace(strUser, #"^(.*?)[\t\n\v\f\r\\/<> ,.#](.+)$", "$2");
// it all depends on your input

Categories

Resources