简体   繁体   中英

Get selected file from Desktop C#

I would like to get the files path of the files selected from the user on the desktop, with this code I can get all file from the explorer

public void GetListOfSelectedFilesAndFolderOfWindowsExplorer()
    {
          Shell exShell = new Shell();
            List<string> selected = new List<string>();
            foreach (ShellBrowserWindow window in (IShellWindows)exShell.Windows())
            {
                // check both for either interface to work
                // add an Exit for to just work the first explorer window 
                if ((window.Document as IShellFolderViewDual) != null)
                {
                    foreach (FolderItem fi in window.Document.SelectedItems)
                        selected.Add(fi.Path);
                }
                else if ((window.Document as ShellFolderView) != null)
                {
                    foreach (FolderItem fi in window.Document.SelectedItems)
                        selected.Add(fi.Path);
                }
            }
    }

I need to get the files names of the files in the desktop, but the list result empty.

the files paths selected is not clear for me, but try the below code to get the list of files and folders in the user desktop

// Get the path of the user's desktop
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        // Get the paths of all files and directories on the desktop
        string[] files = Directory.GetFiles(desktopPath);
        string[] directories = Directory.GetDirectories(desktopPath);

        // Create a string to store the paths
        string message = "";

        // Add all files to the message
        foreach (string file in files)
        {
            message += file + "\n";
        }

        // Add all directories to the message
        foreach (string directory in directories)
        {
            message += directory + "\n";
        }

        // Show the message in a message box
        MessageBox.Show(message, "Paths of files and directories on desktop", MessageBoxButtons.OK, MessageBoxIcon.Information);

To get list of selected folders or files, first you have to select the folders or files you need, and this must be done using a tool like FolderBrowserDialog and OpenFileDialog.

Here is an example of how you might use the OpenFileDialog to allow the user to select multiple files on the desktop:

DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            // Get the paths of the selected files
            string[] selectedFiles = openFileDialog1.FileNames;

            // Add all files to the list box
            foreach (string file in selectedFiles)
            {
                message += file + "\n";
            }
        }

And for selecting folders you can use FolderBrowserDialog

 DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            // Get the path of the selected directory
            string directory = folderBrowserDialog1.SelectedPath;
            message =  directory;
        }

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