2009-12-20 67 views
29

如何通过一个按钮如何从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");  

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

+0

你的问题不清楚。你能详细说明吗? “打开文件的属性”是什么意思? – 2009-12-20 19:23:27

+1

你的意思是你想显示该文件的Windows资源管理器属性表,对吧? – 2009-12-20 19:23:45

+0

hello agian, 是我想打开的文件像windows的属性右键单击文件,你可以打开文件的属性 – 2009-12-20 19:29:45

回答

41

解决办法是:

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

当我通过VS 2010通过它来运行该代码时,它可以工作,但是只要我通过构建可执行文件运行它,它就不会工作。这真的很奇怪..它甚至不会在VS中运行,如果我打F5(没有手动通过代码)..任何建议? – wasatchwizard 2012-10-15 19:40:55

+0

有没有办法修改这个弹出的属性窗口没有得到重点?我想把它放在命令行应用程序中,但不会失去命令行窗口的焦点。另外,在返回之前让函数等待窗口关闭会很有趣。 – MemphiZ 2013-03-29 22:44:31

+1

我正在做一些类似于命令行应用程序的事情,虽然它在GUI中点击按钮运行正常,它只是在应用程序调用它时立即自动解锁应用程序。有什么方法让代码在关闭调用它的命令行窗口之前等待属性窗口关闭?我尝试将'.fMask'改为'SEE_MASK_INVOKEIDLIST + SEE_MASK_NOCLOSEPROCESS'(并且增加了它的整数值,也是64),但没有任何改变。 – 2014-12-30 07:37:42

7

各种文件属性可从FileInfo类:

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

呼叫的Process.Start,使含有该文件的名称的ProcessStartInfo,并与ProcessStartInfo.Verb设置为properties。 (有关详情,请参阅非托管SHELLEXECUTEINFO结构,这是什么的ProcessStartInfo包装,并且特别是lpVerb构件的描述。)

+1

作品,但如此hacky – 2009-12-20 19:51:05

+8

可以扩大你为什么认为这是哈克? ProcessStartInfo/ShellExecuteEx是调用诸如“打开”,“打印”和“显示属性”之类的shell动作的标准方式。过去有一种更直接的方式,SHObjectProperties,但是它从Vista开始就被删除了,因此,据我所知,ShellExecuteEx仍然是记录的方法......可以纠正! – itowlson 2009-12-20 20:02:34

+6

我试过这个: var startInfo = new ProcessStartInfo(FileFullPath); startInfo.UseShellExecute = true; startInfo.Verb =“properties”; Process.Start(startInfo); 似乎不起作用;我得到一个Win32Exception“没有应用程序与此操作的指定文件关联” – 2010-06-24 19:36:25

相关问题