简体   繁体   中英

System.IO.File.Copy Error with file not found

I am trying to create a image upload that can copy the image into a folder. The problem is that when I try to copy the photo to a folder, there is an error which says that it could not find the file. My code is below:

private void btn_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            //
            // The user selected a folder and pressed the OK button.
            // We print the number of files found.
            //
            string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);

        }

        if (result == DialogResult.Cancel)
        {
            return;
        }

            //001: Check the user selected a file and pressed ok button. Before opening the dialog
            //      set the filters
            dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";

            string path = folderBrowserDialog1.SelectedPath;
            if (dlgFileOpen.ShowDialog() == DialogResult.OK)
            {
               // string file = Path.GetFileName(dlgFileOpen.FileName);
                string file = Path.GetFileName(dlgFileOpen.FileName);
                //string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
                string newFileName = System.IO.Path.Combine(path, file);
                System.IO.File.Copy(file, newFileName, true);


                //image directory to be saved in database
                //txtSelectedFile.Text = newFileName;
            }
        }


    private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
    {
        string Required_Ext = ".jpeg .jpg .bmp";
        string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
        int index = Required_Ext.IndexOf(selected_ext);
        //002: Inform the user to select correct extension
        if (index < 0)
        {
            MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
            e.Cancel = true;
        }
    }

ANSWER:

private void btn_Click(object sender, EventArgs e)

    {

        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            //
            // The user selected a folder and pressed the OK button.
            // We print the number of files found.
            //
            string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);

        }

        if (result == DialogResult.Cancel)
        {
            return;
        }

            //001: Check the user selected a file and pressed ok button. Before opening the dialog
            //      set the filters
            dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";

            string path = folderBrowserDialog1.SelectedPath;
            if (dlgFileOpen.ShowDialog() == DialogResult.OK)
            {
               // string file = Path.GetFileName(dlgFileOpen.FileName);
                string file = Path.GetFileName(dlgFileOpen.FileName);
                //string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
                string newFileName = System.IO.Path.Combine(path, file);
                System.IO.File.Copy(dlgFileOpen.FileName, newFileName, true);

                txtSelectedFile.Text = newFileName;
                MessageBox.Show("Image uploaded successful!");
            }
        }


    private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
    {
        string Required_Ext = ".jpeg .jpg .bmp";
        string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
        int index = Required_Ext.IndexOf(selected_ext, StringComparison.CurrentCultureIgnoreCase);
        //002: Inform the user to select correct extension
        if (index < 0)
        {
            MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
            e.Cancel = true;
        }
    }

I think you're losing the full file path on the source file.

string file = Path.GetFileName(dlgFileOpen.FileName);  // "file" is the file name
string newFileName = System.IO.Path.Combine(path, file);
System.IO.File.Copy(file, newFileName, true);

Replace this:

System.IO.File.Copy(file, newFileName, true);

With this:

System.IO.File.Copy(dlgFileOpen.FileName, newFileName, true);

Regarding your other issue, from the comments:

"Even though i set the filer with jpeg, how come 1 picture of mine which is jpeg will trigger my error message?"

It's most likely a case-sensitivity issue. Your code will only allow lower-case file extensions; if the file you select is ".JPEG" (all uppercase) it will fail and display the error message.

You can ignore case by calling the overloaded method that accepts a StringComparison type:

int index = Required_Ext.IndexOf(
                selected_ext, StringComparison.CurrentCultureIgnoreCase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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