获取应用程序路径

3

好的,我正在将一些Winform项目从4.0框架迁移到4.5框架,并且在尝试从类库中查找可执行路径时遇到了问题。我有一个错误日志记录类,用于记录错误,下面是我的代码片段:

using System;
using System.IO;
using System.Windows.Forms;

namespace FunctionClassLibrary
{
    public static class ErrorLogging
    {
        public static string errLogFullPath
        {
            get
            {
                string errLogFile = "Application";
                m_errLogFullPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), errLogFile + "_ErrorLog.txt");
                return m_errLogFullPath;
            }
        }
    }
}

如您所见,我能够引用 System.Windows.Forms 命名空间,进而使用 Application.ExecutablePath 属性。但在 Framework 4.5 中,您无法再引用 System.Windows.Forms 命名空间。我已经搜索了整个上午,但没有找到解决方法。这两个 MSDN 链接:LINK1LINK2 都展示了使用 System.Windows.Forms 命名空间的示例。所以我的问题是,有没有人有解决此问题的方法?

**更新**

好的,我快要疯了,我刚刚意识到我可以简单地使用 Environment.GetFolderPath 方法来查找 CommonProgramFiles 文件夹,这也是我应该存储这个文件的地方。很抱歉今天早上打扰大家。

2个回答

5
你可以使用Directory.GetCurrentDirectory()AppDomain.CurrentDomain.BaseDirectory代替:
Path.Combine(Directory.GetCurrentDirectory(), 
                 errLogFile + "_ErrorLog.txt");

或者:

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                 errLogFile + "_ErrorLog.txt");

2
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),errLogFile + "_ErrorLog.txt");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接