简体   繁体   中英

OpenFileDialog only loads files from the root

I have an C# application to load some files into my database, but when I try to load the file the application only load from one location (C:), but I need to be able to load the files from any location. I use this function to load the files

private void cmdArchivoTotal_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog dialogoArchivo = new OpenFileDialog();

    dialogoArchivo.InitialDirectory =
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    dialogoArchivo.Filter = "CSV Files (*.csv)|*.csv";

    if (dialogoArchivo.ShowDialog().Value)
        txtArchivoTotal.Text =
            System.IO.Path.GetFullPath("\\"+dialogoArchivo.SafeFileName);             
}

At first I was thinking this was for run the application in debug mode, but even deployed the application only load the files from "C:\\".

How can I load files from any disk and directory?

You're using OpenFileDialog.SafeFileName , which only returns the filename, not the path. By prepending \\ , you're constricted to reading files from the current disk's root.

Just use the FileName property, which contains the full path:

 txtArchivoTotal.Text = dialogoArchivo.FileName

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