Save file with openFileDialog

Asked

Viewed 650 times

1

I need to select PDF files with the openFileDialog and save them in a specific directory set in a string in Properties.Settings, but the code below does not work.

private void btAnexarArquivo_Click(object sender, EventArgs e)
    {
        // Displays an OpenFileDialog so the user can select a Cursor.  
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Arquivo PDF|*.pdf";
        openFileDialog1.Title = "Selecione o arquivo PDF";

        // Show the Dialog.  
        // If the user clicked OK in the dialog and  
        // a .CUR file was selected, open it.  
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string diretorio = openFileDialog1.InitialDirectory;
            string destino = Properties.Settings.Default.Pasta + ticket + ".pdf";
            FileInfo oFile = new FileInfo(destino);
            if (oFile.Exists)
            {
                oFile.Delete();
            }

            // To move a file or folder to a new location:
            System.IO.File.Copy(diretorio, destino);
        }  
    }

Displays the following error:

Código de Erro

What do I do to fix?

  • How is the result of the variables diretorio and destino, could post please?

  • The variable diretorio I picked up from openFileDialog1.InitialDirectory; I believe there’s the mistake, and the destino would be as an example: \\192.168.10.116\MarcaBus\Arquivos\Comprovantes\arquivo.pdf

  • 1

    Change openFileDialog1.InitialDirectory; for openFileDialog1.FileName;

  • The problem is in this penFileDialog1.Initialdirectory line, it has to be Filename.

2 answers

2


Try it this way:

using System.IO;

private void btAnexarArquivo_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog()
    {
        Filter = "Arquivo PDF|*.pdf",
        Title = "Selecione o arquivo PDF"
    };

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string destino = Path.Combine(Properties.Settings.Default.Pasta, ticket + ".pdf");

        if (File.Exists(destino))
            File.Delete(destino);

        File.Copy(openFileDialog1.FileName, destino);
    }
}

I optimised your code a little bit :)

The problem was on the property InitialDirectory, that should be FileName.

  • The line string destino = Path.Combine(Properties.Settings.Default.Pasta, $"{ticket}.pdf"); is wrong in $"{ticket}.pdf"

  • 1

    Yeah, string interpolation with "$" is only available from . NET Framework 4.6 if I’m not mistaken. Edited answer.

  • O acesso ao caminho '\\192.168.10.116\MarcaBus\Arquivos\Comprovantes\6tm2uicqky.pdf' foi negado. Now the error is in the permissions, right?

  • 1

    Yes, that would be permissions.

  • Is there any way to solve it? I set the directory as Writable by Webmin

  • If you run Visual Studio or EXE as Administrator, you cannot overcome the problem?

Show 2 more comments

1

The class OpenFileDialog owns a property called FileName, it contains the complete directory + the name of the selected file.

Change your code to use FileName instead of InitialDirectory, in the variable declaration diretorio:

string diretorio = openFileDialog1.FileName;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.