AHF
AHF

Reputation: 1072

How to save data from form in a specific drive

I want to save my text file in a F drive but this file is written to a default folder of program . How to save it by guiding a path

string[] contents = new string[2];
contents[0] = "Name: " + textBox1.Text;
contents[1] = "age: " + textBox2.Text;
string path = @"F:\\"; // path to file
System.IO.File.WriteAllLines(textBox1.Text + ".txt", contents);

Upvotes: 1

Views: 83

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98750

As an alternative, you can use File.Move after you created it like;

File.WriteAllLines(textBox1.Text + ".txt", contents);
File.Move(Directory.GetCurrentDirectory() + textBox1.Text + ".txt",
          path + textBox1.Text + ".txt");

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101681

Because you defining a path,but you don't use it.

string path = @"F:\" + textBox1.Text + ".txt";
File.WriteAllLines(path, contents);

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941377

It would be a good idea to actually use your path variable:

  string path = System.IO.Path.Combine(@"F:\", textBox1.Text + ".txt");
  System.IO.File.WriteAllLines(path, contents);

Upvotes: 2

Related Questions