How to use Shell32 within a C# application?

扶醉桌前 提交于 2019-11-26 06:39:23

问题


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


回答1:


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

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



回答2:


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




回答3:


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



回答4:


  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.




回答5:


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.




回答6:


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


        }
    }



回答7:


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.

http://mike-ward.net/2008/09/02/a-lean-method-for-invoking-com-in-c/ https://github.com/mike-ward/DeskDrive/blob/master/src/DeskDrive/Shell32.cs




回答8:


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.




回答9:


Referencing the actual shell32.dll is deprecated. You will get errors in .NET Framework 4+. Using an older .NET Framework just to use the shell32.dll limits your program's capabilities. In applications for windows 7+ and .NET Framework 4+ you should always use the .COM component instead. Right click project. Click Add reference. Click .COM tab in Add reference dialogue. Select Microso.ft Shell Controls and Automation. Click OK



来源:https://stackoverflow.com/questions/5708434/how-to-use-shell32-within-a-c-sharp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!