-2

I need to get the path of the currently selected File or Folder in Windows Explorer to put the ListView. I do not know how to do hope you can help.Thank you

Update Source

public void GetListFileAndFolderOfWindowsExploer()
{
    try
    {
        string fileName;

        ArrayList selected = new ArrayList();
        Shell32.Shell shell = new Shell32.Shell();

        foreach (SHDocVw.InternetExplorer windows in new SHDocVw.ShellWindows())
        {
            fileName = Path.GetFileNameWithoutExtension(windows.FullName).ToLower();

            if (fileName.ToLowerInvariant() == "explorer")
            {
                Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)windows.Document).SelectedItems();

                foreach (Shell32.FolderItem item in items)
                {
                    lift = new string[] { item.Name, item.Path };

                    ListViewItem list = new ListViewItem();
                    list.Text = item.Name;
                    list.SubItems.Add(item.Path);
                    list.UseItemStyleForSubItems = true;
                    listView1.Items.Add(list);
                }
            }
        }
    }
    catch (Exception ex)
    {
        writelog(ex.Message);
    }
}
3
  • how do you select your file? Jan 29, 2013 at 6:50
  • more details pls, what are you trying to accomplish? there can be more than one windows explorer windows open at a time or none.
    – Adi
    Jan 29, 2013 at 6:57
  • 1
    what have you tried?
    – Venson
    Jan 29, 2013 at 7:03

3 Answers 3

2

You can use an OpenFileDialog(Home and learn OpenFileDialog).

Hope this link helps.

OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Help";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
     string dirName =
     System.IO.Path.GetDirectoryName(fdlg.FileName);
     string drive =
     dirName.Split(System.IO.Path.VolumeSeparatorChar)[0];
     MessageBox.Show(dirName);
     MessageBox.Show(drive);
}
1

To in order to get selected items you have to use the following interfaces:

IServiceProvider
IShellBrowser
IFolderView
IShellFolder2
IPersistFolder2 

or directly

(IEnumIDList and LPITEMIDLIST) foreach all selected items

This works fine in Windows 10.

1

Your question seems unclear,Hope you are using OpenFileDialog for selecting files,

If you're looking for the file path:

string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt

If you're looking for the directory path:

string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder

In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.

1
  • Do I write 1 program nho.va take into Context Menu.when I R-click the file or folder, the program will be run and it will take the path of the folder or file.
    – Babyboypk
    Jan 29, 2013 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.