0

My question is about to delete a shortcut from all user's desktops.

I've updated my Dektop folder from C:\Users\\[User]\Desktop to G:\Users\\[User]\Desktop because I've some important data on desktop and I don't want to lose any user data if I re-install windows or my windows(any how) get corrupted. I've also updated documents and downloads folder to save the data to a drive other than '%SystemDrive%`.

I've done this by - Open WindowsExplorer

-> Right click on Desktop (at the left panel and under the Quick Access list)

-> Properties

-> Location

-> Write new desktop folder location in textbox

-> Apply

-> OK.

Everything works fine, but when I want to delete a shortcut from all user's desktops, I get users folder only from C drive.

My code for deleting shortcut looks like

foreach (var userFolder in userFolders)   //userFolders contains all sub directories of user directory
{
    var shortcutFullName = userFolder + "\\Desktop\\" + shortcutName;

    if (File.Exists(shortcutFullName))
    {
        File.Delete(shortcutFullName);
    }
}

I've tried How do you get the Default Users folder and

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
Inaam ur Rehman
  • 470
  • 1
  • 6
  • 23

1 Answers1

0

You should be able to get Desktop folder using Win32 APIs.

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

const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19;

const int CSIDL_DESKTOP = 0x0;

public static void GetCommonProfilePath()
{
    StringBuilder allUserProfile = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_DESKTOPDIRECTORY, false);

    string commonDesktopPath = allUserProfile.ToString();
    //The above API call returns: C:\Users\Public\Desktop 
    Console.WriteLine(commonDesktopPath);
 
 
    SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_DESKTOP = 0, false);

    // This should give you user specific path.
    Console.WriteLine(allUserProfile.ToString());
}

Refer this blog for more details.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37