调用“运行”命令

时间:2012-09-09 10:59:10

标签: c# .net windows

我正在尝试调用通常位于“开始”菜单上的“运行”对话框 - 我做了一些研究,并且只设法找到一种方法来访问它(使用“Windows Key”+ R)。

所以我假设模拟击键,例如:

SendKeys.Send("{TEST}") 

会做这个工作吗?虽然如何模拟键盘上的“Windows”键?

我确信有一种更简单的方法 - 不使用sendkeys - 任何人都有任何想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用PInvoke调用“运行”对话框。

[Flags()]
public enum RunFileDialogFlags : uint
{

    /// <summary>
    /// Don't use any of the flags (only works alone)
    /// </summary>
    None = 0x0000,    

    /// <summary>
    /// Removes the browse button
    /// </summary>
    NoBrowse = 0x0001,

    /// <summary>
    /// No default item selected
    /// </summary>
    NoDefault = 0x0002,

    /// <summary>
    /// Calculates the working directory from the file name
    /// </summary>
    CalcDirectory = 0x0004,

    /// <summary>
    /// Removes the edit box label
    /// </summary>
    NoLabel = 0x0008,

    /// <summary>
    /// Removes the separate memory space checkbox (Windows NT only)
    /// </summary>
    NoSeperateMemory = 0x0020
}

我们需要使用DllImport属性导入DLL。

[DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "#61", SetLastError = true)]

static extern bool SHRunFileDialog(IntPtr hwndOwner, 
                                   IntPtr hIcon, 
                                   string lpszPath,
                                   string lpszDialogTitle, 
                                   string lpszDialogTextBody, 
                                   RunFileDialogFlags uflags);

<强>实施

private void ShowRunDialog(object sender, RoutedEventArgs e)
{
    SHRunFileDialog(IntPtr.Zero, 
                    IntPtr.Zero, 
                    "c:\\",
                    "Run Dialog using PInvoke",
                    "Type the name of a program, folder or internet address 
            and Windows will open that for you.",
                    RunFileDialogFlags.CalcDirectory);

}