どのように私はアプリケーションの現在のフォルダは知っていますか? ...実行中のコードからどこにexeファイルがあるのか知る方法はありますか?事前にコンパクトなフレームワークの現在のフォルダ
おかげ
どのように私はアプリケーションの現在のフォルダは知っていますか? ...実行中のコードからどこにexeファイルがあるのか知る方法はありますか?事前にコンパクトなフレームワークの現在のフォルダ
おかげ
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
string fullAppPath = Path.GetDirectoryName(fullAppName);
は、Windows Mobileは、現在のフォルダの概念がありません。 "現在のフォルダ"は、アプリケーションがどこにあるかに関係なく、基本的に常にファイルシステムのルートに設定されています。
は、アプリケーションが配置されているパスは、あなたがAssembly.GetExecutingAssembly()
を使用することができます取得するには、および
CodeBase
プロパティやメソッド
GetName()
システムを戦うしないでください。
マイクロソフトでは、アセンブリ以外のプログラムファイルフォルダを使用しないようにしています。設定ファイルは、アプリケーションデータ、ファイルの保存など、ユーザーがマイドキュメントに移動する際に知っておく必要があります。
jalfの回答はうまくいくが、あなたはシステムと戦っている。彼らがあなたのアセンブリがどのフォルダにあるのかを知りたい本当に良い理由がない限り、私はそれに反対します。
あなたは、以下のサンプルでGetModuleFileName
、exeファイルの場所を返し、GetStartupPath
はexeファイルのディレクトリを返すGetExecutablePath
方法を使用することができます。
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);
}
}
次は正しいです。
string fullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
fullAppPath = Path.GetDirectoryName(fullAppName);
他の言語の同等のコードについてはこちら
link
これは、このような小さな爪のための本当に大きなハンマーです。 – ctacke