如何从C#显示文件的"属性"对话框?

Pow*_*fet 32 c#

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

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

谢谢.

例如,如果想要系统属性

Process.Start("sysdm.cpl");    
Run Code Online (Sandbox Code Playgroud)

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

Pow*_*fet 46

解决方案是:

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);
}
Run Code Online (Sandbox Code Playgroud)


ito*_*son 12

调用的Process.Start,传递的ProcessStartInfo包含文件的文件名,并与ProcessStartInfo.Verb设置properties.(有关更多信息,请参阅非托管SHELLEXECUTEINFO结构的说明,这是ProcessStartInfo包装的内容,特别是lpVerb成员.)

  • 可以扩展为什么你认为它是hacky?ProcessStartInfo/ShellExecuteEx是调用shell操作的标准方法,如"open","print"和"show properties".以前有一种更直接的方式,SHObjectProperties,但是从Vista开始就删除了,所以就我所知,ShellExecuteEx仍然是文档化的方法...可以打开更正! (8认同)
  • 我试过这个:var startInfo = new ProcessStartInfo(FileFullPath); startInfo.UseShellExecute = true; startInfo.Verb ="properties"; 的Process.Start(StartInfo的); 似乎不起作用; 我得到一个Win32Exception"没有应用程序与此操作的指定文件相关联" (6认同)
  • 它不起作用,不适用于 exe、jpeg、mp3 或其他任何东西!这些都没有名为property的动词!或显示属性! (2认同)

Mic*_*tta 7

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

FileInfo info = new FileInfo(path);
Console.WriteLine(info.CreationTime);
Console.WriteLine(info.Attributes);
...
Run Code Online (Sandbox Code Playgroud)