gpt4 book ai didi

c# - XmlTextWriter 路径错误?

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:10 27 4
gpt4 key购买 nike

下面的语句有什么问题?

XmlTextWriter writer = new XmlTextWriter(@"D:\project\data\" + System.DateTime.Today + @"\" + System.DateTime.Now + ".xml", null);

当我尝试上面的语句时,它给出了以下错误

The given path's format is not supported.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: The given path's format is not supported.

最佳答案

撇开非法字符不谈,您尝试做的事情是不可能的,原因很简单:只要 XmlTextWriter 创建文件(如果文件不存在),它不会创建目录。这就是您想要做的:

XmlTextWriter writer = new XmlTextWriter(
/* your root path */ @"D:\project\data\" +
/* NEW directory */ System.DateTime.Today + @"\" +
/* new file name */ System.DateTime.Now + ".xml", null);

您需要手动为指定日期创建目录:

var path = string.Format(@"D:\project\data\{0:yyyyMMdd}", DateTime.Now);
// if directory already exists nothing will happen
Directory.CreateDirectory(path);

或者将日期合并到文件名中:

var fileName = string.Format(@"D:\project\data\{0:yyyyMMdd}_{0:HHmmssfff}.xml",
DateTime.Now);

关于c# - XmlTextWriter 路径错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9275709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com