Hi,
i am facing a problem in c#.net i have a user data form where i have used picture box and opendailogbox when i am selecting a picture it is showing in picturebox now what i want is that i have a button on the form when i click on that button picture should be automatically store in my solution image folder???
help me!

hi!

try this:

string oldfilename = openfiledlg.Filename // e.g. c:/test/test.bmp - name from your opendialog
string newfilename = Path.Combine(Application.StartupPath,Path.GetFileName(oldfilename ));
File.Copy(oldfilename ,newfilename);

i hope there are no typing -errors :)

Daniel

You don't want to do that. Typically Application.StartupPath will point to your C:\Program Files\Vendor Name\Product Name\ directory. If you write to that directory with Vista or later it will actually redirect the filesystem write to C:\Users\username\something by a process known as virtualization. You should use Application.UserAppDataPath instead for profile-level data storage per application.

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmOpenDlg : Form
  {
    public frmOpenDlg()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      using (OpenFileDialog ofd = new OpenFileDialog())
      {
        ofd.Title = "Image selection";
        ofd.FileName = "";
        ofd.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
        ofd.Multiselect = false;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
          string newPath = Path.Combine(Application.UserAppDataPath, Path.GetFileName(ofd.FileName));
          pictureBox1.Image = Image.FromFile(ofd.FileName);
          if (File.Exists(newPath))
          {
            //You could rename the file
            MessageBox.Show("That image already exists...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          else
          {
            File.Copy(ofd.FileName, newPath);
            if (MessageBox.Show("Would you like to view the image directory?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
              System.Diagnostics.Process.Start(@"explorer.exe", "\"" + ofd.FileName + "\"");
            }
          }
        }
      }
    }

  }
}

Thanks alot
DanielGreen & sknake both of you helped me now its working properly thanks alot...

This question has already been answered. Start a new discussion instead.