Search code examples
c#imagepathopenfiledialogfile-copying

C#: OpenFileDialog copy a file


Hi I have a button with this code:

     OpenFileDialog ofd = new OpenFileDialog();
        string percorso = Environment.CurrentDirectory;
        percorso += @"\img\Articoli";
        ofd.InitialDirectory = percorso;
        ofd.Filter = "File JPEG/JPG (*.jpeg *.jpg)|*.jpg;*.jpeg|File PNG (*.png)|*.png;|Tutti i files (*.*)|*.*"; 
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            tPercorso.Text = System.IO.Path.GetFileName(ofd.FileName);
            Refresh_Image();
        }       

I need that when I select a file in the OpenFileDialog, if his path is not equal to "percorso" a MessageBox will appear: "Do you want to copy file in default path?" YES | NO if DialogResult == YES I need to copy this file to path "percorso" and select filename only like I'm doing. Can you help me? Regards.


Solution

  • If you only care about copying the file, look no further than File.Copy. If you want to copy the file with the standard shell progress dialog, check out this related question.

    You should also check out Path.Combine.