[英]How to create Directory/File into IIS using ASP.NET C# Entity Framework
My work environment is:我的工作环境是:
When I debug in Visual Studio, the directory/file is created without problem inside this path ~/Content/Documentacion/log/
(using IIS Express that comes with VS).当我在 Visual Studio 中调试时,在此路径
~/Content/Documentacion/log/
内创建目录/文件没有问题(使用 VS 附带的 IIS Express)。
But when I publish the solution and run it using IIS 7.5, the directory/file is not created, I cannot understand the problem.但是当我发布解决方案并使用 IIS 7.5 运行它时,没有创建目录/文件,我无法理解这个问题。
This is the code:这是代码:
public void CreaLogATiempos(DateTime fin, bool sobrescribir = false)
{
try
{
text = "xxxxx= ";
string docPath = "~/Content/Documentacion/log/" ;
var folder = System.Web.HttpContext.Current.Server.MapPath(docPath );
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
FileInfo MyFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(docPath + this.archivo));
if (!MyFile.Exists)
{
StreamWriter crear = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(docPath + this.archivo));
crear.WriteLine(text);
crear.Close();
}
else
{
StreamWriter crear = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(docPath + this.archivo), true);
crear.WriteLine(text);
crear.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
Maybe someone can see the error or has an idea about the problem?也许有人可以看到错误或对问题有所了解?
You can use this folder in your controller as following way您可以按以下方式在 controller 中使用此文件夹
var physicalPath = Server.MapPath(Settings.Default.CompanyImagePath);
Can I introduce you to TextWriterTraceListener
?我可以向您介绍
TextWriterTraceListener
吗? Add this to your Global.vb file:将此添加到您的Global.vb文件中:
'adding tracing to a log.
Trace.Listeners.Clear()
Dim stream As New IO.FileStream(Server.MapPath("~/App_Data/trace.log"), IO.FileMode.Append)
Dim writer As New IO.StreamWriter(stream, System.Text.Encoding.UTF8)
Dim logListener As New TextWriterTraceListener(writer, "trace.log")
Trace.Listeners.Add(logListener)
Trace.AutoFlush = True
Trace.WriteLine($"{DateTime.Now.ToString} - class: Global.asax - method: Application_Start - message: Trace listener loaded.")
Create a trace.log file inside "~/App_Data"
在
"~/App_Data"
中创建一个trace.log文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.