Current Compact Framework folder

How to find out what is the current application folder? I mean ... Is there a way to find out where the exe is from the current code?

early

+6
c # windows-mobile compact-framework
source share
5 answers
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase; string fullAppPath = Path.GetDirectoryName(fullAppName); 
+6
source share

Windows Mobile does not have the concept of the current folder. The "current folder" is basically always set as the root of the file system, regardless of where your application is located.

To get the path to your application, you can use Assembly.GetExecutingAssembly() , and the CodeBase or GetName() property

+8
source share

Do not fight the system.

Microsoft does not want you to use the program files folder for anything other than assemblies. Configuration files should be stored in the "Application data", "Save files and the like" section, which users should know about "My Documents".

jalf answer will work, but you are struggling with the system. If they are not a really good reason why you want to know what folder your assembly is in, I would suggest against it.

+4
source share

You can use GetModuleFileName

In the example below, the GetExecutablePath method returns the location of the exe, and GetStartupPath returns the directory of the exe.

 using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; using System.Text; class Program { [DllImport("coredll", SetLastError = true)] public static extern uint GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, [MarshalAs(UnmanagedType.U4)] int nSize); [DllImport("coredll")] public static extern uint FormatMessage([MarshalAs(UnmanagedType.U4)] FormatMessageFlags dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, out IntPtr lpBuffer, uint nSize, IntPtr Arguments); [DllImport("coredll")] public static extern IntPtr LocalFree(IntPtr hMem); [Flags] internal enum FormatMessageFlags : uint { AllocateBuffer = 256, FromSystem = 4096, IgnoreInserts = 512 } public static string GetModuleFileName(IntPtr hModule) { StringBuilder lpFilename = new StringBuilder(short.MaxValue); uint num = GetModuleFileName(hModule, lpFilename, lpFilename.Capacity); if (num == 0) { throw CreateWin32Exception(Marshal.GetLastWin32Error()); } return lpFilename.ToString(); } private static Win32Exception CreateWin32Exception(int error) { IntPtr buffer = IntPtr.Zero; try { if (FormatMessage(FormatMessageFlags.IgnoreInserts | FormatMessageFlags.FromSystem | FormatMessageFlags.AllocateBuffer, IntPtr.Zero, (uint)error, 0, out buffer, 0, IntPtr.Zero) == 0) { return new Win32Exception(); } return new Win32Exception(error, Marshal.PtrToStringUni(buffer)); } finally { if (buffer != IntPtr.Zero) { LocalFree(buffer); } } } public static string GetStartupPath() { return Path.GetDirectoryName(GetExecutablePath()); } public static string GetExecutablePath() { return GetModuleFileName(IntPtr.Zero); } } 
+2
source share

Correctly.

 string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase; fullAppPath = Path.GetDirectoryName(fullAppName); 
For equivalent code in other languages ​​See Link
+2
source share

All Articles