I have a web project like:
namespace Web
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbResult.Text = PathTest.GetBasePath();
}
}
}
The method PathTest.GetBasePath()
is defined in another Project like:
namespace TestProject
{
public class PathTest
{
public static string GetBasePath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
}
Why it's display ...Web
while the TestProject assembly is compiled into bin
folder(in other words it should display ...Webin
in my thought).
Now I got a troublesome if I modified method into:
namespace TestProject
{
public class FileReader
{
private const string m_filePath = @"File.config";
public static string Read()
{
FileStream fs = null;
fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + m_filePath,FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
return reader.ReadToEnd();
}
}
}
The File.config
is created in TestProject. Now AppDomain.CurrentDomain.BaseDirectory + m_filePath
will returen ..WebFile.config
(actually the file was be copied into ..WebinFile.config
), an exception will be thrown.
You could say that I should modified m_filePath
to @"inFile.config"
. However If I use this method in a Console app in your suggest, AppDomain.CurrentDomain.BaseDirectory + m_filePath
will return ..ConsoleinDebuginFile.config
(actually the file was copyed into .ConsoleinDebugFile.config
), an exception will be thrown due to surplus bin
.
In other words, in web app, AppDomain.CurrentDomain.BaseDirectory
is a different path where file be copyed into (lack of /bin
), but in console app it's the same one path.
Any one can help me?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…