40

What should I include within a C# application in order to make Shell32 work?

Edit:

My application can't recognize shell32. What references or lib should I include? What I'm trying to do is:

Shell32.Shell shell = new Shell32.Shell(); 

What I'm getting as an error:

Error 1 The type or namespace name 'Shell32' could not be found (are you missing a using directive or an assembly reference?)

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Lisa
  • 3,121
  • 15
  • 53
  • 85
  • Shell32.Shell shell = new Shell32.Shell(); Error 1 The type or namespace name 'Shell32' could not be found (are you missing a using directive or an assembly reference?) – Lisa Apr 18 '11 at 20:29
  • Looks like you are trying to use an external library. Shell32 is not a standard component of .NET framework. Have you copied the sample from somewhere? – Andrei Marukovich Apr 18 '11 at 20:30
  • @David I believe what OP is looking for is "what using statements are required..." @Shaza, if this is correct you need to look at P/Invoke. Shell32 is a invoked using reflection as an unmanaged .dll. More can be found here: http://stackoverflow.com/questions/2133703/referencing-shell32-again-c-visual-studio – Cos Callis Apr 18 '11 at 20:33

9 Answers9

63

Just add a reference to Shell32.dll from the Windows\System32 folder and use it:

Shell32.Shell shell = new Shell32.Shell();
shell.MinimizeAll();
Dudi Keleti
  • 2,946
  • 18
  • 33
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
  • I am using 64 bit OS and buiding app as x86, so should I take reference from same Windows/system32 or from "C:\Windows\SysWOW64". Shell32.dll size is diff in both folder. – Romil Kumar Jain Jul 20 '14 at 04:37
  • 14
    You can also add a reference to *Microsoft Shell controls and Automation* from the COM tab in the *Reference Manager* (*Add Reference* Dialog Window) in Visual Studio 2010 and above... not sure which earlier versions have this though. – Sheridan Feb 13 '15 at 11:19
  • @Sheridan: My "Microsoft Shell Controls And Automation" reports it is Version 1.0 from Windows 7 SP1. So I assume this is at least available as far back as Win7 SP1. – Joe B Dec 05 '16 at 17:24
  • @JoeB _Windows 10 Creator Update_ here, still version 1.0 – T-moty Jun 22 '17 at 10:11
  • Adding a reference to _Microsoft Shell controls and Automation_ is equivalent to adding _shell32.dll_ from "Windows\System32". The resulting reference is the same and contains only the Guid of the DLL. – Andrius R. Aug 18 '21 at 12:52
60

maybe this can help:

  1. Right click project
  2. Click Add reference
  3. Click .COM tab in Add reference dialogue
  4. Select Microsoft Shell Controls and Automation
  5. Click OK

your shell32 is ready to use...

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
32

I know this thread is old, but I post this for anyone having the same problem as I did. The solution above does not compile under windows 8

Shell32.Shell shell = new Shell32.Shell(); <= this doesn't work with windows 8

Use the work around below if you want your apps to run under windows 8.

using Shell32;

private Shell32.Folder GetShell32Folder(string folderPath)
{
    Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
    Object shell = Activator.CreateInstance(shellAppType);
    return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
    System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folderPath });
}
Du D.
  • 5,062
  • 2
  • 29
  • 34
  • 1
    I had COM errors when I was experimenting on Windows 10 Tech Preview but this code fixed it. Thanks – Mog0 Oct 03 '14 at 22:50
  • 4
    Your answer looks *very* much like the answer from the [Instantiate Shell32.Shell object in Windows 8](https://social.msdn.microsoft.com/Forums/vstudio/en-US/b25e2b8f-141a-4a1c-a73c-1cb92f953b2b/instantiate-shell32shell-object-in-windows-8?forum=clr) question in the Visual Studio Forum. While your sentiment of updating this question is welcome, showing someone else's work without providing a link to it is called plagiarism and is *not* welcome on Stack Overflow. Please edit your answer and provide some kind of attribution if this is the case. – Sheridan Feb 13 '15 at 11:17
  • 12
    I definitely did not intended to plagiarize the answer, I just don't remember where I got it from, beside I don't think I got it from there. A quick search on google, this solution pop up everywhere and some of them is even older than the link you provided. I don't know what to say to that. http://techitongue.blogspot.ca/2012/06/shell32-code-compiled-on-windows-7.html – Du D. Feb 13 '15 at 15:53
  • 2
    Fair enough, but in future, please provide attribution when showing other people's work. – Sheridan Feb 14 '15 at 18:08
  • This should be the accepted answer. It works fine on Windows 10. – golden96371 Jun 07 '21 at 14:28
8
  1. Right click your project in the solution explorer.
  2. Choose "Add Reference..." from the drop-down menu.
  3. Click the "Browse" tab.
  4. Navigate to the C:\Windows\System32 directory.
  5. Choose the shell32.dll file. and press the "OK" button.

You now have the appropriate reference for using Shell32.Shell.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
5

Add to your project a reference to the COM library Microsoft Shell Controls and Automation. Additionally, ensure that the code using Shell32.Shell is running in a single threaded apartment, e.g. by adding the [STAThread] attribute to Main.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
3

I'm guessing that you're having trouble getting any calls recognized, so I'd refer you to this general article: http://www.codeproject.com/KB/shell/csdoesshell1.aspx

Beyond that, you'll need to provide specifics of what isn't working for you.

holtavolt
  • 4,378
  • 1
  • 26
  • 40
2

The class shown below should help with some of the methods of shell32 in C# . you should add the reference of "Microsoft Shell command and automation" with the reference window by righting clicking the project .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    namespace MusicMuttPrototype
    {
        public class clsShellFileInfo
        {
            public Exception errorException;
            public enum FileDetailInfo
            {
                Name = 0,
                Year = 15,
                Size = 1,
                Track_Number = 19,
                Type = 2,
                Genre = 20,
                Date_Modified = 3,
                Duration = 27,
                Date_Created = 4,
                Bit_Rate = 28,
                Date_Accessed = 5,
                Protected = 23,
                Attributes = 6,
                Camera_Model = 24,
                Status = 7,
                Date_Picture_Taken = 25,
                Owner = 8,
                Dimensions = 26,
                Author = 9,
                Not_used = 27,
                Title = 10,
                Not_used_file = 28,
                Subject = 11,
                //Not_used = 29,
                Category = 12,
                Company = 30,
                Pages = 13,
                Description = 31,
                Comments = 14,
                File_Version = 32,
                Copyright = 15,
                Product_Name_Chapter = 33,
                //Scripting Quicktest Profess11ional Page 63 
                Artist = 16,
                Product_Version = 34,
                Album_Title = 17,
                Retrieves_the_info_tip_inf = -1
            }

            public string getFileDetails(string fileFolder, string filePath, FileDetailInfo infotype)
            {
                string strReturnval = "";
                try
                {
                    Shell32.Shell fileshell = new Shell32.Shell();
                    Shell32.Folder fileshellfolder = fileshell.NameSpace(fileFolder);
                    Shell32.FolderItem Item = fileshellfolder.ParseName(filePath);
                    strReturnval = fileshellfolder.GetDetailsOf(Item, (int)infotype);
                }
                catch (Exception ex)
                {

                    errorException = ex;
                }
                return strReturnval;
            }


        }
    }
2

If you don't need the full set of API calls, you maybe better off creating a COM import stub class. See how Mike Ward who wrote Desk Drive did it.

Link 1

Link 2

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
noztol
  • 494
  • 6
  • 25
  • i like this. i found a msdn tutorial on this, and a ms tool to generate them. the page is called: "COM Interop Part 1: C# Client Tutorial", in case my link breaks. and here is the link: https://msdn.microsoft.com/en-us/ie/aa645736%28v=vs.94%29?f=255&MSPPError=-2147217396 – Heriberto Lugo Aug 31 '19 at 18:41
1

C# .Net I do not understand the full question but this is working for me.

[DllImport("Shell32.dll")]
        public static extern int ShellExecuteA(int hwnd, string lpOperation, string lpFile, int lpParameters, int lpDirecotry, int nShowCmd);

private void pictureBox1_Click(object sender, EventArgs e)
        {
            int open;
            open = ShellExecuteA(0, "open", "https://google.com", 0, 0, 1);
        }

Using a picturebox to open a link.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Tim Flinn
  • 61
  • 1
  • 6