The_Little_Cousin
The_Little_Cousin

Reputation: 13

How to use text from a TextBox as a file name?

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

Answers (2)

David P
David P

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

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

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

Related Questions