Search code examples
c#winformsdesktop-application

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 enter image description here

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

Solution

  • 2 Approaches to Solve this problem


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