2012-02-20 94 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)。这对你有用吗?

相关问题