Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I write a code in c# to invoke Windows+R,the Shell window in a console application?

What I have tried:

Shell32.Shell shell = new Shell32.Shell();
shell.FileRun();
Posted
Updated 3-Nov-20 11:10am
Comments
Richard MacCutchan 3-Nov-20 12:21pm    
What exactly are you trying to achieve?

1 solution

I have no idea why you want to do such a thing, but I found where you got that code from and followed ALL OF THE INSTRUCTIONS with the code snippet and it worked just fine.

I'm guessing you either skipped adding the COM reference to the "Microsoft Shell Controls and Automation" library, probably because you didn't understand what that meant, or you didn't follow the instruction to add the [STAThread] attribute to the method you made the call to FileRun() in.

C#
using System;

namespace CsSandbox
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Shell32.Shell shell = new Shell32.Shell();
            shell.FileRun();
        }
    }
}

You can add the reference by going to the Solution Explorer in your project, right-clicking the References item, and select "Add Reference..." In the dialog that comes up, click on the COM item on the left side of the dialog, then scroll down the long list of COM libraries that is displayed until you find "Microsoft Shell Controls and Automation". Hover the mouse on that item and check the checkbox next to it. Click OK and you're done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900