繁体   English   中英

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

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

我的工作环境是:

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

当我在 Visual Studio 中调试时,在此路径~/Content/Documentacion/log/内创建目录/文件没有问题(使用 VS 附带的 IIS Express)。

但是当我发布解决方案并使用 IIS 7.5 运行它时,没有创建目录/文件,我无法理解这个问题。

这是代码:

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);
    }
}

也许有人可以看到错误或对问题有所了解?

  • Select Visual Studio 中的项目,然后按 ALT + Enter 打开该项目的设置属性,如下图

图片

您可以按以下方式在 controller 中使用此文件夹

var physicalPath = Server.MapPath(Settings.Default.CompanyImagePath);

我可以向您介绍TextWriterTraceListener吗? 将此添加到您的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.")

"~/App_Data"中创建一个trace.log文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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