DateTime.Now.ToString("M/d/yyyy HHmmss") returns 月仅在部署位置

DateTime.Now.ToString("M/d/yyyy HHmmss") returns month only in deployed location

正在创建新文件夹

string newFolder = Globals.applicationPath + @"BatchHistory\Batch" + DateTime.Now.ToString("M/d/yyyy HHmmss") + @"\";

在开发 PC 中一切正常,文件夹命名为:"Batch12-23-2019 181504"

将控制台应用程序部署到服务器时,文件夹名称显示为 "Batch12"

有什么线索吗?

侧面细节:

The issue was in fact culture settings on deployed location.

您的 DateTime.Now.ToString() 方法 可能 也在您的部署位置创建 "some" 格式,但正斜杠 (/) 不能 是 Windows 中文件夹名称的一部分。

发件人:Naming Files, Paths, and Namespaces

    The following reserved characters:
        < (less than)
        > (greater than)
        : (colon)
        " (double quote)
        / (forward slash)
        \ (backslash)
        | (vertical bar or pipe)
        ? (question mark)
        * (asterisk)

当您尝试命名文件夹时,它看起来像是在 "first" / 字符部分之前分配的,在您的例子中是 Batch12

记住,the "/" custom format specifier has a special meaning of "replace me current culture or supplied culture date separator" on custom date strings. In your dev pc, your CurrentCulture's DateSeparator property 似乎是 - 这就是它出现在结果字符串中的原因。

这意味着您部署的服务器的 CurrentCulture 正在使用 / 作为 DateSeparator,这就是为什么它 仍然 结果 /当你使用 ToString 方法时。

我建议您在开发和部署的服务器上使用相同的文化设置。或者您可以在代码中将日期分隔符从 / 更改为 -

string newFolder = Globals.applicationPath + @"BatchHistory\Batch" +
                   DateTime.Now.ToString("M-d-yyyy HHmmss") + @"\";