-5

I have a window which has two textboxes namely Username and Password and two buttons namely Save and Close. I want to create a .txt file when the save button is clicked.The name of the text file should be the Username and have details of username and password in it. So every time when I save with a different Username it creates a text file with the username as file name.

string fileName = textBox1.Text + ".txt";
File.Create("C:\\Users\\User\\Documents\\1ClickData\\ID\\fileName.txt");

The above code is not working for me. Please help.

Also the textboxes are transparent. I don't want them to be transparent. Please help me out in this also. Thank you.

Raymond
  • 396
  • 4
  • 21
  • 1
    this is not that difficult actually there are literally 1000's of working examples online in regards to how to create a text file in C# you could have saved yourself a lot of time by performing a google search first.. – MethodMan May 03 '16 at 14:46
  • All examples in google are creating file with a predefined filename. So I asked here @MethodMan – Aashish Kumar Shaw May 03 '16 at 14:52
  • Textbox transparency should perhaps be a different question? String formatting, interpolation, and concatenation are very basic stuff. – DVK May 03 '16 at 14:56
  • WOW @AashishKumarShaw you should re-read your comment it makes zero sense.. you need to take the timeout to read some .net basic tutorials – MethodMan May 03 '16 at 14:57
  • I am a noob at c#. I am seeking online tutorials but I didn't find this anywhere so I asked. I also searched in stackoverflow but all example where creating a predefined filename. If you don't want to help its okay. But don't create arguments here. @MethodMan – Aashish Kumar Shaw May 03 '16 at 15:10
  • once again you are not understanding parameters if they had predefined filenames, you take the examples that work and make them work for your use case.. do not use the excuse that you are a `noob at C#` regardless you need to understand the basics respectfully speaking no arguments here just stating the pure facts.. – MethodMan May 03 '16 at 15:14

2 Answers2

2

Try this, if you want to save username password in the same file

string fileName = textBox1.Text + ".txt"; 
System.IO.File.WriteAllText(@"C:\\Users\\User\\Documents\\1ClickData\\ID\\" + fileName, "Username="+textBox1.Text+" Password="+textBox2.Text);

or if you want to save username and password in separate file

    //for username
    string fileName1 = textBox1.Text + ".txt"; 
    System.IO.File.WriteAllText(@"C:\\Users\\User\\Documents\\1ClickData\\ID\\" + fileName1, textBox1.Text);

    //for password
    string fileName2 = textBox2.Text + ".txt"; 
    System.IO.File.WriteAllText(@"C:\\Users\\User\\Documents\\1ClickData\\ID\\" + fileName2, textBox2.Text);
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
0

Your code is creating a file called fileName.txt

What you want to be doing is using the variable instead of the text fileName. There are multiple ways to do this:

File.Create("C:\\Users\\User\\Documents\\1ClickData\\ID\\" + fileName);

This is the simplest way given your variable's value. You can also also use String.Format and interpolation

File.Create(String.Format("C:\\Users\\User\\Documents\\1ClickData\\ID\\{0}", fileName)); //String.Format
File.Create($"C:\\Users\\User\\Documents\\1ClickData\\ID\\{fileName}"); //interpolation
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26