By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

How to create text file with a name base on a text box.

P: 6
I am writing a small application for my company. There are two text boxes and a submit button. textBox1 is where the information is entered to be write to the text file. TextBox2 is where the the user is suppose to enter the name of the file. Problem is I can't get get Textbox 2 to work. The file is created with the static name in the code but I can't figure out how to make the name it self using TextBox2. Where is my code for this section. As you can see the value entered into TextBox2 is assigned to the file name variable. I just don't know how to add static information to:
StreamWriter sw = new StreamWriter(filename);

Expand|Select|Wrap|Line Numbers
  1.         string filename;     
  3.         filename = TextBox2.Text;
  5.         StreamWriter sw = new StreamWriter(filename);
  6.         sw.WriteLine(textBox1.Text);
  7.         sw.Flush();
  8.         sw.Close();
Dec 11 '08 #1
Share this Question
Share on Google+
9 Replies

Curtis Rutland
Expert 2.5K+
P: 3,256
What do you mean by "static information"?

You should just be able to grab text from the textbox the way you just did.
Dec 11 '08 #2

Expert Mod 5K+
P: 9,731
Have you considered using an OpenFileDialog control to retrieve the correct path/file name that the user wants to work with?

Are you getting any error messages?
Or are you just having problems retrieving the file info?
Dec 11 '08 #3

P: 6
I want the path the file will be uploaded to an the file extension to be static and they just type in the name of the file.
Dec 11 '08 #4

Expert Mod 5K+
P: 9,731
Have you looked at using a masked text box?
Dec 11 '08 #5

P: 6
No, How does that work?
Dec 11 '08 #6

Expert Mod 5K+
P: 9,731
Check it out :D

Dec 11 '08 #7

Curtis Rutland
Expert 2.5K+
P: 3,256
Or define the path and extension in code, and request the filename.
Expand|Select|Wrap|Line Numbers
  1. string path = @"c:\test\";
  2. string extension = ".txt";
  3. string fullpath = path + TextBox2.Text + extension
Dec 12 '08 #8

P: 6
Thanks insertAlias, that will work.
Dec 12 '08 #9

Expert 2.5K+
P: 3,525
May I throw in a couple suggestions to 'beef up' the function for the real world?

Don't assume you have a C: drive. Silly as it might sound, it is possible to set up a PC where the boot drive is J: or some other letter. It doesn't cost anything to ask the OS for its SystemDrive and build your string from there.

Don't assume the directory hierarchy exists at write-time. It does no harm to make the directory again, just in case someone decided to do some hard drive clean up and deleted what was an empty directory at that time.

Don't assume you have rights to the directory you're writing to. What happens if someone changes the path to read-only or takes away your permissions to write to it? You could start writing a file that never gets created.

Don't assume the user only enters "Filename" in the text box. What if the user enters "Filename.txt" in the text box? You don't need a file named "Filename.txt.txt" because you blindly added '.txt' to whatever they entered.
Personally I like this construct because it fixes a bad extension and adds one if needed
Expand|Select|Wrap|Line Numbers
  1. string FileNameVariable = Path.ChangeExtension(FileNameVariable, ".txt");
And you probably want to parse the file name the user enters to strip out any illegal filename characters like ? * / and so on.

In short don't assume anything except that every condition you're expecting will be wrong, and that anything the user can do to foul up your program will be done.
Dec 14 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.