【问题标题】:Save file to a specific folder in Windows Form C#将文件保存到 Windows Form C# 中的特定文件夹
【发布时间】:2020-05-18 14:50:35
【问题描述】:

我正在尝试将一些选定的文件保存在我的应用程序内的文件夹(图像)中

我可以使用以下代码获取文件:

         private void button1_Click(object sender, EventArgs e)
    {

        string imagelocation = "";

        OpenFileDialog dialog = new OpenFileDialog();

        if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )
        {
            textBox2.Text = dialog.FileName;
        }
    }

为了保存我在 textBox2 中得到的文件,我尝试了以下代码。但是使用以下代码,我还必须选择要保存文件的路径。 如果我想(将我的路径永久设置为 'images' 文件夹,如图所示)保存怎么办?

       private void button2_Click(object sender, EventArgs e)
    {


        SaveFileDialog f = new SaveFileDialog();

        if(f.ShowDialog() == DialogResult.OK)
        {
            using(Stream s = File.Open(f.FileName, FileMode.CreateNew))
            using(StreamWriter sw =  new StreamWriter(s))
            {
                sw.Write(textBox2.Text);
            }
        }
     }

【问题讨论】:

标签: c# winforms desktop-application


【解决方案1】:

解决这个问题的2种方法


  • 第一种方法: (浏览文件并点击保存,自动将所选文件保存到图像目录)


    private void button2_Click(object sender, System.EventArgs e)
    {

        var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        var assemblyParentPath = Path.GetDirectoryName(assemblyPath);
        var imageDir = Path.Combine(assemblyParentPath, "Image");

        if (!Directory.Exists(imageDir))
            Directory.CreateDirectory(imageDir);


         using (Stream s = File.Open(imageDir+"\\"+Path.GetFileName(textBox1.Text), FileMode.CreateNew))
         using (StreamWriter sw = new StreamWriter(s))
         {
                     sw.Write(textBox1.Text);
         }

     }


  • 第二种方法:(浏览文件并保存打开 SaveDialog,其中目录为图像目录,文件名为先前选择的文件)
private void button2_Click(object sender, System.EventArgs e)
        {

            var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var assemblyParentPath = Path.GetDirectoryName(assemblyPath);
            var imageDir = Path.Combine(assemblyParentPath, "Image");

            if (!Directory.Exists(imageDir))
                Directory.CreateDirectory(imageDir);

            SaveFileDialog f = new SaveFileDialog();
            f.InitialDirectory = imageDir;
            f.FileName = textBox1.Text;
            if (f.ShowDialog() == DialogResult.OK)
            {
                using (Stream s = File.Open(imageDir + "\\" + Path.GetFileName(textBox1.Text), FileMode.CreateNew))
                using (StreamWriter sw = new StreamWriter(s))
                {
                    sw.Write(textBox1.Text);
                }

            }
        }```

【讨论】:

  • 为什么不设置RestoreDirectory 属性或将InitialDirectory 设置为以前用OpenFileDialog 打开的路径? SaveFileDialog 不见了,也许 OP 需要它,因为它以前就在那里......显然,OP 只是希望打开保存对话框已经设置为使用打开对话框选择的相同路径。
  • 不要手动连接路径。请使用System.IO.Path.Combine(),它更安全且无错误。
  • 嗨,Shashwath,它是否解决了您的问题,还是您正在寻找其他任何东西?
  • @Jimi,使用InitialDirectory 使用另一种方法更新了解决方案,现在SaveDialog 以特定Directory 和特定File 打开
  • @ja72,我用Path.Combine 代替ImageDir 并且只在File.Open 中使用了`\`,我认为在这种情况下应该没问题
【解决方案2】:

这段代码对你有用吗?

private void button1_Click(object sender, EventArgs e)
{
    var dlg = new OpenFileDialog();
    dlg.Title = "Select Picture";
    dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
    dlg.Filter = "Common Picture Files|*.gif;*.png;*.bmp;|All Files|*.*";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = dlg.FileName;
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多