C# 如何将表单中的数据保存到特定驱动器中
我想将文本文件保存在C# 如何将表单中的数据保存到特定驱动器中,c#,.net,winforms,C#,.net,Winforms,我想将文本文件保存在F驱动器中,但该文件已写入程序的默认文件夹。如何通过引导路径来拯救它 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",
F
驱动器中,但该文件已写入程序的默认文件夹。如何通过引导路径来拯救它
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);
实际上使用路径变量是个好主意:
string path = System.IO.Path.Combine(@"F:\", textBox1.Text + ".txt");
System.IO.File.WriteAllLines(path, contents);
因为您定义了一条路径,但没有使用它
string path = @"F:\" + textBox1.Text + ".txt";
File.WriteAllLines(path, contents);
作为替代方案,您可以在创建后使用它,如
File.WriteAllLines(textBox1.Text + ".txt", contents);
File.Move(Directory.GetCurrentDirectory() + textBox1.Text + ".txt",
path + textBox1.Text + ".txt");