C# 获取应用程序路径
好的,我将一些Winform项目从Framework4.0迁移到Framework4.5,在试图从类库中找到可执行路径时遇到了一个问题。我有一个用于记录错误的错误日志类。下面是我的代码片段:C# 获取应用程序路径,c#,winforms,entity-framework,path,C#,Winforms,Entity Framework,Path,好的,我将一些Winform项目从Framework4.0迁移到Framework4.5,在试图从类库中找到可执行路径时遇到了一个问题。我有一个用于记录错误的错误日志类。下面是我的代码片段: using System; using System.IO; using System.Windows.Forms; namespace FunctionClassLibrary { public static class ErrorLogging { public sta
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链接都显示了使用System.Windows.Forms命名空间的示例。所以我的问题是,有没有人能解决这个问题
**更新**
好的,我正在失去理智,我刚刚意识到我可以简单地使用Environment.GetFolderPath方法来查找CommonProgramFiles文件夹,这是我应该存储此文件的地方。很抱歉今天早上打扰大家。您可以使用
目录.GetCurrentDirectory()
或AppDomain.CurrentDomain.BaseDirectory
:
Path.Combine(Directory.GetCurrentDirectory(),
errLogFile + "_ErrorLog.txt");
或:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
errLogFile + "_ErrorLog.txt");
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),errLogFile + "_ErrorLog.txt");