简体   繁体   English

如何使用 ASP.NET C# 实体框架将目录/文件创建到 IIS

[英]How to create Directory/File into IIS using ASP.NET C# Entity Framework

My work environment is:我的工作环境是:

  • Windows 7 Professional Windows 7 专业
  • Visual Studio 2019, ASP.NET MVC, Entity Framework Visual Studio 2019,ASP.NET MVC,实体框架
  • SQL Server Express SQL 服务器快递
  • IIS Version 7.5 (installed locally) IIS 版本 7.5(本地安装)

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?也许有人可以看到错误或对问题有所了解?

  • Select the project in visual studio and press ALT + Enter to open the settings property of that project as following picture Select Visual Studio 中的项目,然后按 ALT + Enter 打开该项目的设置属性,如下图

Picture图片

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM