tôi có một dự án web như:Tại sao AppDomain.CurrentDomain.BaseDirectory không chứa "bin" trong ứng dụng asp.net?
namespace Web
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbResult.Text = PathTest.GetBasePath();
}
}
}
Phương pháp PathTest.GetBasePath()
được định nghĩa trong dự án khác như:
namespace TestProject
{
public class PathTest
{
public static string GetBasePath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
}
Tại sao nó hiển thị ...\Web\
trong khi lắp ráp TestProject được biên dịch vào bin
thư mục (hay nói cách khác nó sẽ hiển thị ...\Web\bin
trong suy nghĩ của tôi).
Bây giờ tôi có một rắc rối nếu tôi sửa đổi phương pháp thành:
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();
}
}
}
Các File.config
được tạo ra trong TestProject. Bây giờ AppDomain.CurrentDomain.BaseDirectory + m_filePath
sẽ trả lại ..\Web\File.config
(thực sự tệp được sao chép vào ..\Web\bin\File.config
), một ngoại lệ sẽ được ném.
Bạn có thể nói rằng tôi nên sửa đổi m_filePath
thành @"\bin\File.config"
. Tuy nhiên, nếu tôi sử dụng phương pháp này trong ứng dụng Console trong đề xuất của bạn, AppDomain.CurrentDomain.BaseDirectory + m_filePath
sẽ trả lại ..\Console\bin\Debug\bin\File.config
(thực sự tệp được sao chép thành .\Console\bin\Debug\File.config
), một ngoại lệ sẽ được ném do số dư bin
.
Nói cách khác, trong ứng dụng web, AppDomain.CurrentDomain.BaseDirectory
là một đường dẫn khác mà tệp được sao chép vào (thiếu /bin
), nhưng trong ứng dụng bảng điều khiển, đó là cùng một đường dẫn.
Bất kỳ ai có thể giúp tôi?
Cơ sở của ứng dụng web là gốc web nơi chứa các trang ASPX. Thư mục bin chỉ là thư mục con của thư mục gốc. – Tejs
Phew! Tôi nghĩ rằng tôi đã phát điên! Tôi đã gặp vấn đề tương tự ... – ECC