Reputation: 13
I am a beginner when it comes to c#. Can anyone help me?
I want the variable Username to be used as the filename like .txt. I have this code:
String Username = Nametxt.Text;
StreamWriter stream = new StreamWriter(@"*Username*.txt");
I want the value of Username to be used in (@"Username.txt"), i.e, Username should be replaced with the actual value of Username.
I'd be glad if anyone can help :)
Upvotes: 0
Views: 695
Reputation: 2083
This should solve your issue. You simply concatenate the variables in order to open up the correct file in your StreamWriter.
StreamWriter stream = new StreamWriter(Username + ".txt");
Upvotes: 1
Reputation: 6251
You need to concatenate the username and the file extension like this:
StreamWriter stream = new StreamWriter(Username + ".txt");
Or, you can use String.Format
like this:
StreamWriter stream = new StreamWriter(String.Format("{0}.txt", Username));
Upvotes: 0