【问题标题】:how To get list File And Folder selected in Windows Explorer如何获取在 Windows 资源管理器中选择的列表文件和文件夹
【发布时间】:2013-01-12 16:22:41
【问题描述】:

我需要在 Windows 资源管理器中获取当前选择的文件或文件夹的路径以放置 ListView。我不知道怎么办希望你能帮忙。谢谢你

更新源

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);
    }
}

【问题讨论】:

  • 如何选择文件?
  • 更多细节请,你想完成什么?一次可以打开多个 Windows 资源管理器窗口,也可以不打开。

标签: c# .net directory


【解决方案1】:

您可以使用OpenFileDialog(Home and learn OpenFileDialog)。

希望此链接对您有所帮助。

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);
}

【讨论】:

    【解决方案2】:

    要获得所选项目,您必须使用以下接口:

    IServiceProvider
    IShellBrowser
    IFolderView
    IShellFolder2
    IPersistFolder2 
    

    或直接

    (IEnumIDList and LPITEMIDLIST) foreach all selected items
    

    这在 Windows 10 中运行良好。

    【讨论】:

      【解决方案3】:

      您的问题似乎不清楚,希望您使用OpenFileDialog 选择文件,

      如果您正在寻找文件路径:

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

      如果您正在寻找目录路径:

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

      一般来说,System.IO.Path 类具有许多有用的功能,可用于检索和操作路径信息。

      【讨论】:

      • 我是否将 1 个程序 nho.va 写入上下文菜单中。当我 R-单击文件或文件夹时,程序将运行,它将采用文件夹或文件的路径。跨度>
      猜你喜欢
      • 2014-02-10
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多