简体   繁体   English

将文件保存到 Windows Form C# 中的特定文件夹

[英]Save file to a specific folder in Windows Form C#

I am trying to save some selected file in a folder(images) inside my application我正在尝试将一些选定的文件保存在我的应用程序内的文件夹(图像)中在此处输入图片说明

I am able to get the file using following code:我可以使用以下代码获取文件:

         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;
        }
    }

For saving the file I got in textBox2, I tried following code.为了保存我在 textBox2 中获得的文件,我尝试了以下代码。 But with following code I have to also select the path where I want to save the file.但是使用以下代码,我还必须选择要保存文件的路径。 What If I want to (set my path permanently to 'images' folder as shown in pic) for saving?如果我想(将我的路径永久设置为“图像”文件夹,如图所示)进行保存怎么办?

       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);
            }
        }
     }

2 Approaches to Solve this problem 2 解决这个问题的方法


  • First Approach: (Browser the File and click Save, to automatically save the selected file to Image Directory)第一种方法:(浏览文件并点击保存,将选中的文件自动保存到图像目录)


    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);
         }

     }


  • Second Approach: (Browser the File and Save Opens SaveDialog with Directory as Image Directory and File name as Previously selected File)第二种方法:(浏览文件并保存以目录作为图像目录和文件名作为先前选择的文件打开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);
                }

            }
        }```

Does this code work for you?这些代码在你那正常吗?

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM