【问题标题】:How do I display a file's Properties dialog from C#?如何从 C# 显示文件的属性对话框?
【发布时间】:2009-12-20 19:20:19
【问题描述】:

如何通过按钮打开文件的属性对话框

private void button_Click(object sender, EventArgs e)
{
    string path = @"C:\Users\test\Documents\tes.text";
    // how to open this propertie
}

谢谢。

例如如果想要系统属性

Process.Start("sysdm.cpl");    

但是我如何获得文件路径的属性对话框?

【问题讨论】:

  • 您的问题不清楚。你能详细说明吗? “打开文件的属性”是什么意思?
  • 您的意思是要显示该文件的 Windows 资源管理器属性表,对吗?
  • 你好 agian,我想打开文件属性就像windows一样右键单击一个文件,你可以打开文件的属性

标签: c#


【解决方案1】:

解决办法是:

using System.Runtime.InteropServices;

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHELLEXECUTEINFO
{
    public int cbSize;
    public uint fMask;
    public IntPtr hwnd;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpVerb;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpFile;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpParameters;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpDirectory;
    public int nShow;
    public IntPtr hInstApp;
    public IntPtr lpIDList;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpClass;
    public IntPtr hkeyClass;
    public uint dwHotKey;
    public IntPtr hIcon;
    public IntPtr hProcess;
}

private const int SW_SHOW = 5;
private const uint SEE_MASK_INVOKEIDLIST = 12;
public static bool ShowFileProperties(string Filename)
{
    SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
    info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
    info.lpVerb = "properties";
    info.lpFile = Filename;
    info.nShow = SW_SHOW;
    info.fMask = SEE_MASK_INVOKEIDLIST;
    return ShellExecuteEx(ref info);        
}

// button click
private void button1_Click(object sender, EventArgs e)
{
    string path = @"C:\Users\test\Documents\test.text";
    ShowFileProperties(path);
}

【讨论】:

  • 当我通过 VS 2010 逐步运行此代码时,它可以工作,但每当我通过构建可执行文件运行它时,它就无法工作。这真的很奇怪..如果我按 F5(无需手动单步执行代码),它甚至不会在 VS 中运行.. 有什么建议吗?
  • 有没有办法修改这个弹出的属性窗口没有获得焦点?我想把它放在命令行应用程序中,但不会失去命令行窗口的焦点。让函数在返回之前等待窗口关闭也会很有趣..
  • 我正在使用命令行应用程序做类似的事情,虽然它在 GUI 中通过单击按钮运行良好,但通过命令行它只是在调用它时自动关闭应用程序.有没有办法让代码在关闭调用它的命令行窗口之前等待属性窗口关闭?我尝试将.fMask 更改为SEE_MASK_INVOKEIDLIST + SEE_MASK_NOCLOSEPROCESS(并添加它的整数值64),但没有任何改变。
  • @J.ScottElblein 我只需在 ShowFileProperties 调用之后放置一个 Thread.Sleep(500) 即可在我的应用程序退出后打开窗口并保持打开状态。看起来应用程序必须在创建窗口之前运行。也许这可以通过实际等待标题包含属性的窗口来改善。
【解决方案2】:

调用 Process.Start,传递一个包含文件名的ProcessStartInfo,并将ProcessStartInfo.Verb 设置为properties。 (有关更多信息,请参阅非托管 SHELLEXECUTEINFO 结构的描述,它是 ProcessStartInfo 包装的内容,尤其是 lpVerb 成员。)

【讨论】:

  • 能否详细说明您认为它是 hacky 的原因? ProcessStartInfo/ShellExecuteEx 是调用“打开”、“打印”和“显示属性”等 shell 操作的标准方式。曾经有一种更直接的方法,SHObjectProperties,但从 Vista 开始,它就被删除了,所以据我所知,ShellExecuteEx 仍然是记录在案的方法……欢迎更正!
  • 事先检查 PSI 的 .Verbs 属性。我在 .exe 上尝试过这个,也许 shell 不喜欢这个,但喜欢在其他文件类型上。
  • 它不起作用,不适用于 exe、jpeg、mp3 或其他任何东西!这些都没有一个名为属性的动词!或显示属性!
【解决方案3】:

FileInfo 类提供了各种文件属性:

FileInfo info = new FileInfo(path);
Console.WriteLine(info.CreationTime);
Console.WriteLine(info.Attributes);
...

【讨论】:

    【解决方案4】:

    解决方法是使用ShellExecute () api。

    如何使用 C# 调用此 api: http://weblogs.asp.net/rchartier/442339

    在调试和发布模式下,如果没有 CharSet 属性,这对我来说很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      相关资源
      最近更新 更多