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

时间:2009-12-20 19:20:19

标签: c#

如何通过按钮

打开文件的属性对话框
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");    

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

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:12)

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

答案 2 :(得分:7)

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

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

答案 3 :(得分:0)

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

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

对于我而言,这在调试和发布模式下都没有CharSet属性。