2009-07-09 18 views
6

Come faccio a sapere qual è la cartella corrente di un'App ?? Voglio dire ... C'è un modo per sapere dove si trova l'exe dal codice in esecuzione?Compact Framework Current Folder

grazie in anticipo

risposta

6
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase; 
string fullAppPath = Path.GetDirectoryName(fullAppName); 
8

Windows Mobile non ha il concetto di una cartella corrente. La "cartella corrente" è fondamentalmente sempre impostata come root del filesystem, indipendentemente da dove si trova l'applicazione.

per ottenere il percorso dell'applicazione si trova, è possibile utilizzare Assembly.GetExecutingAssembly(), e la CodeBase proprietà o GetName() metodo

4

non combattono il sistema.

Microsoft non desidera che si utilizzi la cartella dei file di programma per altri elementi diversi dagli assiemi. I file di configurazione dovrebbero andare in Dati applicazioni, Salva file e simili di cui gli utenti devono sapere andare in Documenti.

La risposta di jalf funzionerà ma tu stai combattendo il sistema. A meno che la loro sia davvero una buona ragione per cui vuoi sapere in quale cartella si trova il tuo assembly, ti suggerisco di non farlo.

2

È possibile utilizzare GetModuleFileName

Nel esempio riportato di seguito, il metodo GetExecutablePath restituisce la posizione del exe, e GetStartupPath restituisce la directory del file 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); 
    } 
} 
+0

Questo è un martello davvero grande per un chiodo così piccolo. – ctacke

2

Quello che segue è corretta.

string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase; 
fullAppPath = Path.GetDirectoryName(fullAppName);
Per codice equivalente in altre lingue Fare riferimento a questo link

Problemi correlati