【问题标题】:AppDomain.CurrentDomain.BaseDirectory path issue when running MVC project运行 MVC 项目时 AppDomain.CurrentDomain.BaseDirectory 路径问题
【发布时间】:2015-07-29 17:08:10
【问题描述】:

此代码在 App_Data 文件夹中创建一个日志文件。如果文件不存在,则创建它。当 MVC 页面在我的 IIS 上发布时,它可以工作。

 var filename = AppDomain.CurrentDomain.BaseDirectory + 
                "App_Data//" + 
                label + 
                DateTime.Now.ToString() + 
                ".txt";

 var sw = new System.IO.StreamWriter(filename, true);

但是当我在本地运行项目时,我得到:

NotSupportedException。 StreamWriter 说我的路径不受支持。这是我得到的路径:

“C:\Projects\x- chefsportalen 2.0\Chefsportalen\App_Data//Paragå manuell2015-05-18 11:00:18.txt”

App_Data 文件夹存在且路径正确,为什么会出现这个错误?

【问题讨论】:

  • "App_Data//""` should be @"App_Data\"``

标签: c# file stream


【解决方案1】:

文件名不允许包含“:”字符。

使用显式格式字符串指定不包含“:”的格式,例如

DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture)

我不确定为什么当您发布到 IIS 时它会起作用 - 可能是因为 IIS 使用的文化没有“:”作为时间分隔符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多