2012-02-20 141 views
1

在我的應用程序中,獲取我使用以下代碼的路徑。windows mobile應用程序

Dim path As String 
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly() 
path = System.IO.Path.GetDirectoryName(asm.GetName().CodeBase) 

這給路徑\Application Data\ 但我需要\Program Files\找到.sdf文件。如何獲得這條路?

+2

假設你對你的問題的標題是正確的,在移動環境中'Progam Files'不存在,並且你的數據庫文件應該在你的應用根路徑中。 – balexandre 2012-02-20 08:20:11

回答

0

如果您的Windows Mobile版本下7(窗口響度單位)工作檢查: C#代碼:

public static string GetFolderPath(SpecialFolder folder) 
{ 
     //buffer to fill with path 
     StringBuilder path = new StringBuilder(MaxPath); 

     //pass stringbuilder and folder identifier to api function 
     if(!Convert.ToBoolean(SHGetSpecialFolderPath(IntPtr.Zero, 
     path, (int)folder, 0))) 
     { 
     throw new Exception("Cannot get folder path!"); 
     } 

     return path.ToString(); 
} 

[DllImport("coredll", EntryPoint="SHGetSpecialFolderPath", 
    SetLastError=false)] 
internal static extern int SHGetSpecialFolderPath(IntPtr hwndOwner, 
    StringBuilder lpszPath, int nFolder, int fCreate); 

VB.net代碼:

Public Shared Function GetFolderPath(folder As SpecialFolder) As String 
    'buffer to fill with path 
    Dim path As New StringBuilder(MaxPath) 

    'pass stringbuilder and folder identifier to api function 
    If Not Convert.ToBoolean(SHGetSpecialFolderPath(IntPtr.Zero, path, CInt(folder), 0)) Then 
     Throw New Exception("Cannot get folder path!") 
    End If 

    Return path.ToString() 
End Function 

<DllImport("coredll", EntryPoint := "SHGetSpecialFolderPath", SetLastError := False)> _ 
Friend Shared Function SHGetSpecialFolderPath(hwndOwner As IntPtr, lpszPath As StringBuilder, nFolder As Integer, fCreate As Integer) As Integer 
End Function 

你得Import System.Environment看到特殊文件夾和 Import System.Runtime.InteropServices以查看DllImport

有關詳細信息:http://msdn.microsoft.com/en-us/library/aa446567.aspx#spfiles_topic_03

請,如果它的工作記住我的答案

0

我通常直接使用asm.CodeBase(但不與Windows Mobile)。這對你有用嗎?

相關問題