星期三, 9月 17, 2014

[C#] Console 應用程式路徑

MVA Twenty C# Questions Explained - [04 How do I get the application's path in a C# console app?]

Console 路徑
namespace MVATwentyQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            // 利用 Reflection 來進行
            string ConsolePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            Console.WriteLine(System.IO.Path.GetDirectoryName(ConsolePath));
        }
    }
}
Winform 路徑
private void btnPath_Click(object sender, EventArgs e)
{
    string WinformPath = Application.StartupPath;
    MessageBox.Show(WinformPath);
}

沒有留言:

張貼留言