0

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!

  • 3
    What exactly are you trying to do? You can totally make a filename this way. – Joel May 10 '13 at 23:42
  • What do you mean it doesn't work? It's not at all clear what you are trying to do. – Kirk Woll May 10 '13 at 23:43
  • How can I delete the invaled chars out of the UserName??? – user2371778 May 11 '13 at 00:04
  • Please do not ask you question more than one times! This is another question that you first ask. Please close this Question and open a new one [FAQ](http://stackoverflow.com/faq). Btw: [How to clean a FileName](http://stackoverflow.com/questions/3825433/c-sharp-remove-invalid-characters-from-filename) – Venson May 11 '13 at 00:18
  • np I found it out by myself ty anyways. – user2371778 May 11 '13 at 00:24
  • well nice. Than please close this question with marking you correct answer – Venson May 11 '13 at 00:26

4 Answers4

0

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";
MastErAldo
  • 634
  • 3
  • 12
  • 29
  • so you're saying that adding \ to every single \ and removing the @ will solve the hole thing with the variable in it? – user2371778 May 10 '13 at 23:47
  • I tested it and it says A field initializer cannot reference the nonstatic field, method, or property `Login.PlayerName' – user2371778 May 10 '13 at 23:50
  • If the `UserName` contains [invalid path characters](http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx) no function for creating or using files will accept this string and both of this is ok with `"\\"` oder `@"\"` it meens the same just another way to write it – Venson May 10 '13 at 23:53
  • could you give an example, I don't really understand how I should put it. – user2371778 May 10 '13 at 23:53
0

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

Venson
  • 1,772
  • 17
  • 37
0

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
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0

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(), "");
}
clamchoda
  • 4,411
  • 2
  • 36
  • 74