好的,我正在将一些我的Winform项目从Framework 4.0迁移到Framework 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链接:LINK1和LINK2都显示使用System.Windows.Forms命名空间的示例。所以我的问题是,是否有人有解决此问题的方法?
好的,我正在失去理智,我只是意识到我可以简单地使用Environment.GetFolderPath方法来查找CommonProgramFiles文件夹,这是我应该存储此文件的位置。抱歉打扰每个人今天早上。
答案 0 :(得分:5)
您可以改为使用Directory.GetCurrentDirectory()
或AppDomain.CurrentDomain.BaseDirectory
:
Path.Combine(Directory.GetCurrentDirectory(),
errLogFile + "_ErrorLog.txt");
或者:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
errLogFile + "_ErrorLog.txt");
答案 1 :(得分:2)
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),errLogFile + "_ErrorLog.txt");