C#は「すべてのユーザーデスクトップ」のパスを取得します



C Get Path Ofall User Desktops



転載http://www.cnblogs.com/DoNetCShap/p/4221899.html

C#を使用して、すべてのユーザーのデスクトップ(Public Desktop)へのパスを取得したいと思います。



とてもシンプルだと思いましたが

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

現在のユーザーのデスクトップパスのみを取得できます。結局、C ++関数を呼び出す方法はありません。



[DllImport('shfolder.dll', CharSet = CharSet.Auto)] private static extern int SHGetFolderPath ( IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath ) private const int MAX_PATH = 260 private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019 public static string GetAllUsersDesktopFolderPath ( ) { StringBuilder sbPath = new StringBuilder(MAX_PATH) SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath) return sbPath.ToString() }

DLLのディレクトリを取得します

Assembly myAssembly = Assembly.GetEntryAssembly() string path = myAssembly.Location DirectoryInfo dr = new DirectoryInfo(path) Path=dr.Parent //Upper directory of the current directory

プログラムの現在のパスを取得するためのC#メソッド

System.Environment.CurrentDirectory //Example: c: est Application.ExecutablePath (including name) //Example: c: estmyapp.exe Application.StartupPath (not including the name) //Example: c: est

新しいプロセスコンポーネントを取得し、ファイル名(プロセス名)を含む現在アクティブなプロセスに関連付けるメインモジュールへのフルパス。



string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName Result: X:xxxxxxxxx.exe (the directory where the .exe file is located +.exe file name)

現在のディレクトリ(プロセスが開始されたディレクトリ)の完全修飾パスを取得および設定します。

string str = System.Environment.CurrentDirectory Result: X:xxxxxx (the directory where the .exe file is located)

現在のスレッドの現在のアプリケーションドメインのベースディレクトリを取得します。これは、アセンブリ競合リゾルバーがアセンブリをプローブするために使用します。

string str = System.AppDomain.CurrentDomain.BaseDirectory Result: X:xxxxxx (the directory where the .exe file is located +'')

アプリケーションを含むディレクトリの名前を取得して設定します。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase Result: X:xxxxxx (the directory where the .exe file is located +'')

実行可能ファイルの名前を除いて、アプリケーションを起動した実行可能ファイルへのパスを取得します。

string str = System.Windows.Forms.Application.StartupPath Result: X:xxxxxx (the directory where the .exe file is located)

実行可能ファイルの名前を含む、アプリケーションを起動した実行可能ファイルへのパスを取得します。

string str = System.Windows.Forms.Application.ExecutablePath Result: X:xxxxxxxxx.exe (the directory where the .exe file is located +.exe file name)

アプリケーションの現在の作業ディレクトリを取得します( 信頼できない )。

string str = System.IO.Directory.GetCurrentDirectory() Result: X:xxxxxx (the directory where the .exe file is located) Get system special folder path (favorite, desktop)

1、お気に入りのパス

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites)

2、デスクトップパス

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)

詳細については、列挙型クラスSystem.Environment.SpecialFolderを参照してください。

static void Main(string[] args) { //Environment.GetFolderPath(Environment.SpecialFolder) // This method retrieves a path to a system special folder such as Program Files, Programs, System, or Startup. // can be used to access public information. Special folders are set by the system by default or explicitly set by the user when installing a version of Windows. The //folder parameter specifies the special folder to retrieve, and this parameter must be a value in the Environment.SpecialFolder enumeration any other value will throw an exception. //folder enumeration //ApplicationData directory, which is used as a public repository for application-specific data for current roaming users. // The CommonApplicationData directory, which is used as a public repository for application-specific data used by all users. // LocalApplicationData directory, which is used as a public repository for application-specific data currently used by non-roaming users. // Cookies are used as a directory for public repositories of Internet cookies. // Desktop logical desktop, not physical file system location. // Favorites is the directory used as the public repository for user favorites. // History A directory used as a public repository for Internet history items. // InternetCache is the directory used as a public repository for temporary Internet files. // Programs contains the directory of the user program group. // MyComputer 'My Computer' folder. // MyMusic “My Music” folder. // MyPictures 'My Pictures' folder. // Recent contains a directory of documents that the user has recently used. // SendTo contains the directory for the Send menu item. // StartMenu contains the directory for the Start menu item. // Startup corresponds to the directory of the user's 'startup' program group. // System 'System' directory. // Templates is used as the directory for the public repository of document templates. // DesktopDirectory is used to physically store the directory of file objects on the desktop. // Personal is the directory used as the public repository for the document. // MyDocuments 'My Computer' folder. // ProgramFiles 'Program files' directory. // CommonProgramFiles A directory for components that are shared between applications. Console.WriteLine('Common Repository: {0}', Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) Console.WriteLine('Directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)) Console.WriteLine('Directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) Console.WriteLine('COOKIE path: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Cookies)) Console.WriteLine('Logical Desktop: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) Console.WriteLine('Favorites path: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Favorites)) Console.WriteLine('web browsing history path: {0}', Environment.GetFolderPath(Environment.SpecialFolder.History)) Console.WriteLine ('path to temporary Internet files: {0}', Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)) Console.WriteLine('Application Directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Programs)) Console.WriteLine('My Computer Directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)) Console.WriteLine('My Music Directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)) Console.WriteLine('image directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)) Console.WriteLine('Recently used document directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Recent)) Console.WriteLine('Recently sent file directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.SendTo)) Console.WriteLine ('Directory starting in the boot menu: {0}', Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)) Console.WriteLine('System directory: {0}', Environment.GetFolderPath(Environment.SpecialFolder.System)) Console.WriteLine ('Directory used as a public repository for document templates: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Templates)) Console.WriteLine ('Directory for physically storing file objects on the desktop: {0}', Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)) Console.WriteLine ('Directory used as the public repository for the document: {0}', Environment.GetFolderPath(Environment.SpecialFolder.Personal)) Console.WriteLine('My Computer Folder: {0}', Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) Console.WriteLine('Program files directory. :{0}',Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)) Console.WriteLine ('Directory for components shared between applications: {0}', Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)) Console.ReadLine() }