tags:

views:

659

answers:

3

Hi,

How can I remove a shortcut folder from Startmenu in Windows using C#, I know how to do that using this code:

    private void RemoveShortCutFolder(string folder)
    {
        folder = folder.Replace("\"  ", "");
        folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), folder);
        try
        {
            if (System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.Delete(folder, true);
            }
            else
            {
            }
        }
        catch (Exception)
        {
        }
    }

But the problem that I need to remove one shortcut folder in ALL USERS folder, not the current logged user. Environment.SpecialFolder.StartMenu gives me the current user not all users folder.

Any idea,

Thanks,

A: 

Thanks guys, I found the answer:

    private void RemoveShortCutFolder(string folder)
    {
        folder = folder.Replace("\"  ", "");
        folder = Path.Combine(Path.Combine(Path.Combine(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"), "Start Menu"), "Programs"), folder);
        try
        {
            if (System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.Delete(folder, true);
            }
            else
            {
            }
        }
        catch (Exception)
        {
        }
    }
Happy crashing on non-English systems. You may get away with this on Vista, but definitely not on XP. Unfortunately, I don't know how to do this correctly either :-/
OregonGhost
I was almost thinking of down-voting this. It won't work correctly in many situations (localized Windows versions being only one of them).
Filip Navara
+4  A: 

If you don't mind a little Win32, you can use SHGetSpecialFolderPath.

[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, CSIDL nFolder, bool fCreate);

enum CSIDL
{
  COMMON_STARTMENU = 0x0016,
  COMMON_PROGRAMS = 0x0017
}

static void Main(string[] args)
{
  StringBuilder allUsersStartMenu = new StringBuilder(255);
  SHGetSpecialFolderPath(IntPtr.Zero, allUsersStartMenu, CSIDL.COMMON_PROGRAMS, false);
  Console.WriteLine("All Users' Start Menu is in {0}", allUsersStartMenu.ToString());
}
Samuel
A: 

Hi - I'm not sure the last answer will work on Vista (and Windows 2008?) - ALLUSERSPROFILE will return by default C:\ProgramData however the full path of the Start Menu for all users will be C:\ProgramData\Microsoft\Windows\Start Menu so you'll be missing the Microsoft\Windows part of the path

Thanks,

Dave http://centrel-solutions.com