6
A
回答
6
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
string fullAppPath = Path.GetDirectoryName(fullAppName);
8
的Windows Mobile不具有當前文件夾的概念。無論您的應用程序位於何處,「當前文件夾」基本上始終設置爲文件系統的根目錄。
爲了讓您的應用程序所在的路徑,你可以使用Assembly.GetExecutingAssembly()
,並CodeBase
屬性或方法GetName()
4
不要打製。
Microsoft不希望您將程序文件文件夾用於其他任何裝配。應用程序數據,保存文件等用戶需要知道的有關在我的文檔中進入的配置文件應該存在。
賈爾夫的答案將工作,但你正在與系統作鬥爭。除非他們是一個非常好的理由,爲什麼你想知道你的程序集是在什麼文件夾,那麼我會建議反對它。
2
您可以使用GetModuleFileName
在下面的示例,GetExecutablePath
返回exe文件的位置,並GetStartupPath
返回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
下面是正確的。
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
fullAppPath = Path.GetDirectoryName(fullAppName);
對於其他語言的其他語言的版本其他語言的其他語言版本
link
相關問題
- 1. 當前文件夾
- 2. Compact Framework的 - 將相關性(DLL)的一個子文件夾
- 3. Compact Framework:讀取文件的問題
- 4. .NET Compact Framework的配置文件
- 5. 在Compact Framework中安全擦除文件
- 6. 在Compact Framework中更改文件LastWriteDate
- 7. Rhino Mocks&Compact Framework
- 8. .net compact framework deserialization
- 9. .net Compact Framework 4.0
- 10. .NET Compact Framework
- 11. Indy FTP - 從當前文件夾導航到某個文件夾
- 12. WCF Interceptor On Compact Framework
- 13. .NET Compact Framework的XSockets
- 14. .NET Compact Framework和ActiveSync
- 15. bat文件將子文件夾中的文件移動到當前文件夾
- 16. 獲取當前文件夾名稱
- 17. 缺少當前文件夾Capistrano Rails 3.2
- 18. Ruby中的當前文件夾名稱?
- 19. 當前文件夾是亂碼
- 20. 獲取的當前項目文件夾
- 21. 如何別名當前文件夾
- 22. .BAT當前文件夾的名稱
- 23. 獲取當前文件夾路徑
- 24. 改變當前文件夾中的Node.js
- 25. ASP.NET MVC - 當前文件夾的視圖
- 26. Compact Framework的3.5文本不工作
- 27. Compact Framework中的兩行文本按鈕
- 28. 將文件上傳到當前文件夾上方的文件夾
- 29. Applescript - 將文件從當前文件夾複製到另一個文件夾
- 30. PHP - 在當前文件夾和子文件夾內搜索文件
對於這樣一個小釘子,這是一個非常大的錘子。 – ctacke 2009-07-10 22:17:33