Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何显示文件';从C#中选择s属性对话框?_C# - Fatal编程技术网

如何显示文件';从C#中选择s属性对话框?

如何显示文件';从C#中选择s属性对话框?,c#,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"); 但是如何获取文件路径的属性对话框呢?该类提供了各种文件属性: 调用Pr

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

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

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

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


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

解决方案是:

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

解决方案是使用
ShellExecute()
api

如何使用C#调用此api:


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

您的问题不清楚。你能详细说明一下吗?“打开文件的属性”是什么意思?你是说你想显示该文件的Windows资源管理器属性表,对吗?你好,agian,我想像Windows一样打开文件属性右键单击文件,你可以打开文件的属性可以展开你认为它有问题的原因吗?ProcessStartInfo/ShellExecuteEx是调用诸如“打开”、“打印”和“显示属性”等shell操作的标准方式。过去有一种更直接的方式,即SHObjectProperties,但它从Vista开始就被删除了,因此据我所知,ShellExecuteEx仍然是有文档记录的方法。。。开放更正!事先检查PSI的.Verbs属性。我在一个.exe上试过这个,也许shell不喜欢这个,但喜欢其他文件类型。它不工作,在exe、jpeg、mp3或其他任何文件上都不行!所有这些都没有名为propertise的动词!或显示财产!当我通过VS2010单步运行这段代码时,它可以工作,但每当我通过构建可执行文件运行它时,它就不会工作。真的很奇怪。。如果我点击F5,它甚至不会在VS中运行(没有手动单步执行代码)。。有什么建议吗?有没有办法修改它,使弹出的属性窗口无法获得焦点?我希望将其放在命令行应用程序中,但不要失去命令行窗口的焦点。另外,让函数等待窗口关闭后再返回也会很有趣。我正在使用命令行应用程序执行类似的操作,虽然它在GUI中通过单击按钮运行良好,但通过命令行它只会在调用应用程序时自动关闭应用程序。在关闭调用它的命令行窗口之前,是否有方法让代码等待属性窗口关闭?我尝试将
.fMask
更改为
SEE_MASK\u INVOKEIDLIST+SEE_MASK\u NOCLOSEPROCESS
(并添加了它的整数值64),但没有任何改变。@J.ScottElblein我只需在ShowFileProperties调用后放置一个Thread.Sleep(500),就可以在应用程序退出后打开并保持打开状态。在创建窗口之前,应用程序似乎必须一直运行。也许这可以通过实际等待一个标题包含属性的窗口来改善。@J.ScottElblein如下所示:
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);
}