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 PathTest.GetBasePath() method is defined in another project, for example:
namespace TestProject { public class PathTest { public static string GetBasePath() { return AppDomain.CurrentDomain.BaseDirectory; } } }
Why does it display ...\Web\ , while the TestProject assembly is compiled to the bin folder (in other words, it should display ...\Web\bin in my mind).
Now I am having problems if I modified the method:
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(); } } }
In TestProject, File.config is created. Now AppDomain.CurrentDomain.BaseDirectory + m_filePath will returen ..\Web\File.config (in fact, the file was copied to ..\Web\bin\File.config ), an exception will be thrown.
We can say that I have to change m_filePath to @"\bin\File.config" . However, if I use this method in the Console application in your suggest, AppDomain.CurrentDomain.BaseDirectory + m_filePath will return ..\Console\bin\Debug\bin\File.config (in fact, the file was copied to .\Console\bin\Debug\File.config ), an exception will be thrown due to bin excess.
In other words, in the AppDomain.CurrentDomain.BaseDirectory web application, this is a different path to which the file is copied (missing /bin ), but in the console application it is the same path.
Can anybody help me?
Domi.Zhang Dec 29 2018-11-12T00: 00Z
source share