【问题标题】:Get Application Path获取应用路径
【发布时间】:2012-10-06 16:14:26
【问题描述】:

好的,我正在将一些 Winform 项目从 Framework 4.0 迁移到 Framework 4.5,并且在尝试从类库中查找可执行文件路径时遇到了问题。我有一个用于记录错误的错误记录类,这里是我的代码的 sn-p:

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 文件夹,无论如何我都应该存储这个文件。抱歉今天早上打扰大家了。

【问题讨论】:

    标签: c# winforms entity-framework path


    【解决方案1】:

    您可以改用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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-07
        • 1970-01-01
        • 2011-09-08
        • 2012-04-08
        • 1970-01-01
        • 2012-10-23
        相关资源
        最近更新 更多