在C#中,在saveFileDialog中保存选定路径中的多个文件

时间:2018-01-04 18:02:53

标签: c# silverlight

我正在使用带有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));

如何获取所选文件的路径。

1 个答案:

答案 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();
        }
    }
}