C# - Regarding the Interop.Shell32 Reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • str1ker
    New Member
    • Jun 2006
    • 23

    C# - Regarding the Interop.Shell32 Reference

    Hey guys,

    Sorry if this question is stupid, but I've searched everywhere, but I can't seem to find a solution.

    Basically, I've got this code that makes use of the the Interop.Shell32 .dll. This code is supposed to create shortcuts, .lnk files, desktop or startmenu shortcuts, that kind of thing.

    The shortcut creation code works a treat, but the issue is, I want the application to be standalone, since it is an installer, but when I reference that DLL, it always produces it in the same folder as the application itself.

    When I set 'Copy to Local' to false, and delete the file, I obviously get an error.

    So, if you guys can help, I'd really like one of two possible solutions:
    1) Use the DLL without it having to be alongside the main application.
    2) Create desktop and startmenu shortcuts with a different method.

    This is really confusing me, so I hope you guys can help.

    Thanks in advanced,

    Ashton
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If you are using Interop, it's not standalone.
    What are doing with the Shell32 commands, can you find a way around it?

    Comment

    • str1ker
      New Member
      • Jun 2006
      • 23

      #3
      Originally posted by Plater
      If you are using Interop, it's not standalone.
      What are doing with the Shell32 commands, can you find a way around it?
      Well, like any installer, this one has to create desktop and startmenu shortcuts.

      I've looked everywhere but I can't seem to find a method to create proper desktop shortcuts without using the Interop.Shell32 reference. Here's the code I'm actually using to create the shortcut:
      Code:
      public void createShortcut(string shortcutDirectory, string shortcutName, string targetDirectory, string targetName)
      {
          Shell32.Shell shortcut = new Shell32.ShellClass();
      
          if (folderExists(shortcutDirectory) == false)
              Directory.CreateDirectory(shortcutDirectory);
          StreamWriter shortcutWriter = new StreamWriter(shortcutDirectory + shortcutName + ".lnk", false);
          shortcutWriter.Close();
      
          Shell32.Folder directory = shortcut.NameSpace(shortcutDirectory);
          Shell32.FolderItem item = directory.Items().Item(shortcutName + ".lnk");
          Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)item.GetLink;
      
          link.Path = targetDirectory + targetName;
          link.WorkingDirectory = targetDirectory;
      
          link.SetIconLocation(targetDirectory + targetName, 0);
          link.Save(null);
      }
      And that creates the shortcut in any location you choose, of any file or application you choose.

      There was this thing I did try actually. I added Interop.Shell32 .dll as a resource, and I had it extracted when the program ran. This worked perfectly, except when it came time to delete the DLL again, the DLL was in use, as expected, and I couldn't find a way of releasing it so that I could delete it again.

      Thanks for helping,

      Ashton

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        VS comes with an installer project type that should do this for you?
        Although, I really hate shortcuts that don't let you "find target"

        Comment

        • str1ker
          New Member
          • Jun 2006
          • 23

          #5
          Originally posted by Plater
          VS comes with an installer project type that should do this for you?
          Although, I really hate shortcuts that don't let you "find target"
          Yeah, it does, but I don't really like it. Looks crappy.

          It's no big deal to have the file sitting beside my installer. It would only be nice if it were all standalone.

          I'm definitely not giving up my installer for the given project. I'll probably stick to the method whereby the installer extracts its own Shell32 file at runtime. The file's only about 8KB in size, so having it left there should be no issue.

          I'll probably resolve that in the future.

          Thanks for the help Plater.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Isn't there some sort of "self extracting cabinet" that comes as a .exe and then runs your installer program?

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Originally posted by Plater
              Isn't there some sort of "self extracting cabinet" that comes as a .exe and then runs your installer program?
              Winzip makes a self-extractor program. It's pretty reasonable, and it can automatically execute your setup.exe file when it's done.

              Comment

              • str1ker
                New Member
                • Jun 2006
                • 23

                #8
                Originally posted by insertAlias
                Winzip makes a self-extractor program. It's pretty reasonable, and it can automatically execute your setup.exe file when it's done.
                Oh yeah.

                But I'm not sure how that would help. This installer I've made will be named "setup.exe" , like most.

                What I've noticed about files with that name, is that in zip files, when you double-click that file, when you're using Winzip anyway, it will extract all the files included to a temporary folder, then when everything's done, that's it.

                I suppose creating a self-extractor would be a good idea, since that eliminates any other issues with other possible zip-reading programs, or even no zip reader, just default Windows.

                So, yeah, I'll give that a try, thanks,

                Ashton

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  How big is the .DLL? Can you binary encode it as a resource then extract it back to a .dll file?

                  Comment

                  • str1ker
                    New Member
                    • Jun 2006
                    • 23

                    #10
                    Originally posted by Plater
                    How big is the .DLL? Can you binary encode it as a resource then extract it back to a .dll file?
                    It's tiny.

                    I already said, I included it as a resource, and extracted it when the program ran.

                    The issue is, though, I don't know how to release the DLL so that I can delete it again.

                    Comment

                    Working...