0

I'm not sure if it's possible. I usually create txt files like this:

FileStream fs = new FileStream("c:\\textFile.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

but instead of using "c:\textFile.txt" I want to create a file using a string name. Is there a way to do it?

5
  • Do you want to prompt the user for a file name, or what are you trying to achieve exactly? Commented Aug 18, 2013 at 22:00
  • Unsure what you mean... are you talking about this: new FileStream(myVariable, FileMode.Append, FileAccess.Write); Commented Aug 18, 2013 at 22:00
  • What's a "string name"? Commented Aug 18, 2013 at 22:00
  • Well I'm actually using this in a form application. I have a textBox, and whatever the user inserts into that textBox, I want to create a file from it. Commented Aug 18, 2013 at 22:02
  • Did you even check what parameters does the FileStream constructor take to ask that kind of questions? Commented Aug 18, 2013 at 22:04

2 Answers 2

5

Of course. The first argument to the FileStream constructor takes a string. You've just passed it a string literal (defined in your source code file). It sounds like you want to pass a string variable instead:

string path = // get string from somewhere. A file save dialog, maybe?
FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

May I suggest that you spend some time with a C# tutorial? Microsoft has some good tutorials and samples. With all due respect, and we were all there once, you've got some holes in your knowledge that will trip you up.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure I'm following what you're asking for. You just asked if u could do that:

string filename = "c:\\textFile.txt";
FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

or as you specified

FileStream fs = new FileStream(YourTextBox.Text, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.