如何使用C#从Windows中的Startmenu中删除快捷方式文件夹,我知道如何使用此代码执行此操作:
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)
{
}
}
但问题是我需要删除ALL USERS文件夹中的一个快捷方式文件夹,而不是当前记录的用户。 Environment.SpecialFolder.StartMenu为我提供了当前用户并非所有用户文件夹。
任何想法,
谢谢,
答案 0 :(得分:5)
如果你不介意一点Win32,你可以使用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());
}
答案 1 :(得分:2)
使用Environment.SpecialFolder.CommonStartMenu而不是StartMenu。
答案 2 :(得分:0)
谢谢你们,我找到了答案:
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)
{
}
}