我正在使用带有sliverlight 5的C#。我使用SaveFileDialog来获取用户选择的文件名。现在我需要在不同扩展名的同一位置保存多个文件。我尝试了以下
string directory = System.IO.Path.GetDirectoryName(dialog.SafeFileName);
Stream fileStream = dialog.OpenFile();
StreamWriter sw = new StreamWriter(fileStream);
string sequenceFASTAFileNAme = System.IO.Path.GetFileNameWithoutExtension(dialog.SafeFileName) + ".fa";
string path = System.IO.Path.Combine(directory, sequenceFASTAFileNAme);
if (!File.Exists(sequenceFASTAFileNAme))
{
StreamWriter tw = File.AppendText(System.IO.Path.Combine(directory,sequenceFASTAFileNAme));
tw.WriteLine("The next line!");
tw.Flush();
tw.Close();
}
但我收到了以下错误
不允许进行文件操作。访问路径' helo.fa'被拒绝。
at
StreamWriter tw = File.AppendText(System.IO.Path.Combine(directory,sequenceFASTAFileNAme));
如何获取所选文件的路径。
答案 0 :(得分:-1)
private void button1_Click_1(object sender, EventArgs e)
{
string[] FileV = new string[3];
FileV[0] = "val1";
FileV[2] = "val2";
FileV[3] = "val3";
save(FileV);
}
private void save(string[] FileValue)
{
System.IO.Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
System.IO.StreamWriter tw = System.IO.File.AppendText(saveFileDialog1.FileName + ".ex1");
tw.WriteLine(FileValue[1]);
tw.Flush();
tw.Close();
tw = System.IO.File.AppendText(saveFileDialog1.FileName + ".ex2");
tw.WriteLine(FileValue[2]);
tw.Flush();
tw.Close();
tw = System.IO.File.AppendText(saveFileDialog1.FileName + ".ex3");
tw.WriteLine(FileValue[3]);
tw.Flush();
tw.Close();
myStream.Close();
}
}
}