如何使这组代码将用户输入保存到另一个文件夹中?

本文关键字:另一个 文件夹 保存 输入 何使这 代码 用户 | 更新日期: 2023-09-27 18:18:16

我很失落....如何使这组代码将用户输入保存到另一个文件夹中?(我试图保存。png文件,如果你需要知道的话)

 private void button1_Click(object sender, System.EventArgs e)
 {
     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)
         {
             myStream.Close();
         }
     }
 }

如何使这组代码将用户输入保存到另一个文件夹中?

您还需要编写一些其他或额外的代码,例如,如果用户从一个文件夹中选择一个文件,那么您可以使用System捕获该FilePath。IO和查找如何使用GetFilePath, FileName等…然后,一旦文件被选中,他们想要保存…为什么不使用File.Copy()方法呢?在SO上也有很多其他的例子

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    using( Stream myStream  = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
    using( StreamWriter sw = new TextWriter(myStream) )
    {
        sw.Write("here you can write lines from a file that you read or you can simple write what ever text you are wanting to save to a file this should help you get started" );
    }
}

这里有一个更简单的方法

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    System.IO.File.Copy(saveFileDialog1.Filename, "some dest filePath");
}