415 votes

Comment obtenir un chemin d'accès au bureau pour l'utilisateur actuel en C# ?

Comment puis-je obtenir un chemin d'accès au bureau pour l'utilisateur actuel en C#?

La seule chose que j'ai pu trouver était la classe uniquement disponible en VB.NET SpecialDirectories, qui a cette propriété:

My.Computer.FileSystem.SpecialDirectories.Desktop

Comment puis-je faire cela en C#?

890voto

Marc Gravell Points 482669
string chemin = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

0 votes

Les éléments renvoyés de ce dossier sont différents de ce que Windows Explorer montre. Par exemple, dans mon XP, cela n'inclut pas Mes documents, Poste de travail, Mes lieux réseau, Corbeille et quelques autres raccourcis. Avez-vous une idée de comment obtenir les mêmes entrées que dans Windows Explorer ?

8 votes

Peut-être que vous recherchez SpecialFolder.DesktopDirectory ? Il s'agit du dossier physique au lieu du logique.

2 votes

Cela me renvoie le bureau de l'utilisateur admin si le programme est exécuté en tant qu'administrateur

39voto

Xiaohuan ZHOU Points 139
// Environment.GetFolderPath
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // Données de l'application de l'utilisateur actuel
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); // Données de l'application de tous les utilisateurs
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles); // Fichiers de programme
Environment.GetFolderPath(Environment.SpecialFolder.Cookies); // Cookies Internet
Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Bureau logique
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); // Bureau physique
Environment.GetFolderPath(Environment.SpecialFolder.Favorites); // Favoris
Environment.GetFolderPath(Environment.SpecialFolder.History); // Historique Internet
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); // Cache Internet
Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); // Dossier "Ordinateur"
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Dossier "Mes documents"
Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); // Dossier "Ma musique"
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); // Dossier "Mes images"
Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Dossier "Mes documents"
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); // Dossier Program Files
Environment.GetFolderPath(Environment.SpecialFolder.Programs); // Dossier Programmes
Environment.GetFolderPath(Environment.SpecialFolder.Recent); // Dossier Récent
Environment.GetFolderPath(Environment.SpecialFolder.SendTo); // Dossier "Envoyer vers"
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); // Menu Démarrer
Environment.GetFolderPath(Environment.SpecialFolder.Startup); // Démarrage
Environment.GetFolderPath(Environment.SpecialFolder.System); // Dossier Système
Environment.GetFolderPath(Environment.SpecialFolder.Templates); // Modèles de documents

22voto

bipin Points 81
 chaîne filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
 filePath = filePath +@"\Journal d'erreurs\";
 chaîne extension = ".log";
 si (!Directory.Exists(filePath))
 {
      Directory.CreateDirectory(filePath);
 }

0voto

Gaurav Temkar Points 59
chaîne var = Environment.ExpandEnvironmentVariables("%USERPROFILE%") + @"\Desktop";

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X