8

「\\ProgramFiles\\myApp」のようなアプリのパスを取得したいので、次のコードを使用しようとします。


string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

しかし、最後に「\\myapp.exe」を含むパスが返されます。

私も試しました:


string path = System.IO.Directory.GetCurrentDirectory();

しかし、「NotSupportedException」がスローされます。

最後に.exeなしでパスを取得する方法はありますか?

4

7 に答える 7

19

Application.StartupPathあなたのためにそれをすべきです。

更新: 編集から、Compact Framework で実行されていることがわかります。その場合、Application.StartupPath は機能しません。これは、私が通常使用する構成です。

private static string GetApplicationPath()
{
    return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
于 2009-05-19T06:35:02.793 に答える
11
path = System.IO.Path.GetDirectoryName( path );
于 2009-05-19T06:34:40.250 に答える
3

他のものよりも簡単です:

using System.IO;
using System.Reflection;
...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
于 2009-05-19T06:58:57.013 に答える
1

このためのパラメーターとして元のパス文字列を渡す Path.GetDirectoryName(string) メソッドを使用できます。どのような問題を解決しようとしていますか? たぶん、作業ディレクトリのようなものが本当に必要ですか?

于 2009-05-19T06:36:29.590 に答える
0

あなたの場合のようにexeの場合は、次を使用します。

    // Summary:
    //     Gets the path for the executable file that started the 
    //     application, not including the executable name.    
    Application.StartupPath
于 2009-05-19T06:56:04.490 に答える
0
Path.GetFileNameWithoutExtension(path);
于 2009-05-19T06:36:31.167 に答える
0

FileInfo オブジェクトを使用してディレクトリ名を抽出するのはどうですか?

Vb.Net:

fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
path = fi.DirectoryName
于 2009-05-19T06:36:57.827 に答える