How do I display a file's Properties dialog from C#?

前端 未结 4 2052
无人共我
无人共我 2020-11-28 07:58

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         


        
相关标签:
4条回答
  • 2020-11-28 08:29

    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 = 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 讨论(0)
  • 2020-11-28 08:43

    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.)

    0 讨论(0)
  • 2020-11-28 08:48

    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.

    0 讨论(0)
  • 2020-11-28 08:55

    Various file properties are available from the FileInfo class:

    FileInfo info = new FileInfo(path);
    Console.WriteLine(info.CreationTime);
    Console.WriteLine(info.Attributes);
    ...
    
    0 讨论(0)
提交回复
热议问题