如何在Windows 7的收藏夹中添加文件夹?
我正在开发桌面应用程序我想在收藏夹栏中添加文件夹。如何实现这一功能。
答案 0 :(得分:0)
要获取收藏夹文件夹路径,您需要访问RegistryKey,然后您可以在该目录中创建子文件夹。以下C#代码将在收藏夹文件夹中创建子文件夹。
Microsoft.Win32.RegistryKey rKey = Microsoft.Win32Registry.CurrentUser;
Microsoft.Win32.RegistryKey key = rKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders",
true);
string favoriteFolderPath = key.GetValue("Favorites").ToString();
string myFolder = "MyFolder";
myFolder = Path.Combine(favoriteFolderPath, myFolder);
try
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(myFolder))
{
Directory.CreateDirectory(myFolder);
}
}
catch (Exception)
{
//Could not create Directory
}
答案 1 :(得分:0)
使用JNA,很容易获得 Windows特殊文件夹路径。
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.ShlObj;
import com.sun.jna.platform.win32.WinDef;
// https://github.com/twall/jna#readme
// you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar
public class GetFolderPathDemo {
public static void main(String[] args) {
char[] pszPath = new char[WinDef.MAX_PATH];
Shell32.INSTANCE.SHGetFolderPath(null,
ShlObj.CSIDL_FAVORITES, null, ShlObj.SHGFP_TYPE_CURRENT,
pszPath);
System.out.println(Native.toString(pszPath));
}
}