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

发布于 2024-09-15 19:46:51 字数 357 浏览 14 评论 0原文

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

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

就像windows一样,右键单击一个文件,就可以打开该文件的属性

例如,如果想要系统属性

Process.Start("sysdm.cpl");    

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

How to open an file's Properties dialog by a button

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

Like windows right click on a file and you can open the Properties of the file.

For example if want the System properties

Process.Start("sysdm.cpl");    

But how do I get the Properties dialog for a file path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫竹語嫣☆ 2024-09-22 19:46:51

解决方案是:

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

Solution is:

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 = 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);
}
最笨的告白 2024-09-22 19:46:51

调用 Process.Start,传递包含名称的 ProcessStartInfo文件的,并使用 ProcessStartInfo.Verb 设置为属性。 (有关详细信息,请参阅非托管 SHELLEXECUTEINFO 结构,这是 ProcessStartInfo 所包装的内容,特别是 lpVerb 成员。)

Call Process.Start, passing a ProcessStartInfo containing the name of the file, and with the ProcessStartInfo.Verb set to properties. (For more info, see the description of the unmanaged SHELLEXECUTEINFO structure, which is what ProcessStartInfo wraps, and in particular the lpVerb member.)

寒冷纷飞旳雪 2024-09-22 19:46:51

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

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

Various file properties are available from the FileInfo class:

FileInfo info = new FileInfo(path);
Console.WriteLine(info.CreationTime);
Console.WriteLine(info.Attributes);
...
后知后觉 2024-09-22 19:46:51

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

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

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

Solution is to use ShellExecute () api.

How to invoke this api using C# :
http://weblogs.asp.net/rchartier/442339

This works fine for me without CharSet attribute both in Debug and Release mode.

衣神在巴黎 2024-09-22 19:46:51

为了简化处理 Shell32 等内容,您还可以使用 Vanara ,例如:

using Vanara.PInvoke;
using System.Runtime.InteropServices;
// ...
void ShowProperties(string filepath)
{
    var info = new Shell32.SHELLEXECUTEINFO();

    info.cbSize = Marshal.SizeOf(info);
    info.lpVerb = "properties";
    info.lpFile = filepath;
    info.nShellExecuteShow = ShowWindowCommand.SW_SHOW;
    info.fMask = Shell32.ShellExecuteMaskFlags.SEE_MASK_INVOKEDLIST;

    Shell32.ShellExecuteEx(ref i);
}

并这样称呼它:

ShowProperties(@"C:\The\Path\To\The\File.txt");

To simplify handling Shell32 stuff and such, you could also use Vanara like:

using Vanara.PInvoke;
using System.Runtime.InteropServices;
// ...
void ShowProperties(string filepath)
{
    var info = new Shell32.SHELLEXECUTEINFO();

    info.cbSize = Marshal.SizeOf(info);
    info.lpVerb = "properties";
    info.lpFile = filepath;
    info.nShellExecuteShow = ShowWindowCommand.SW_SHOW;
    info.fMask = Shell32.ShellExecuteMaskFlags.SEE_MASK_INVOKEDLIST;

    Shell32.ShellExecuteEx(ref i);
}

and call it like:

ShowProperties(@"C:\The\Path\To\The\File.txt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文