【问题标题】:How to resolve the exception System.IO.IOException: 'the filename, directory name, or volume label syntax is incorrect in C#?如何解决异常 System.IO.IOException: 'the filename, directory name, or volume label syntax is wrong in C#?
【发布时间】:2021-12-30 15:13:30
【问题描述】:
 public ActionResult Download(DateTime? downloaddate)
    {
        {
            string path1 = AppDomain.CurrentDomain.BaseDirectory + "logs/";
            string filename = "industrialcomm-payload-" + downloaddate + ".txt";
            byte[] filebytes1 = System.IO.File.ReadAllBytes(path1 + filename);
            string contentType1 = "text/plain";

            return File(filebytes1, contentType1);
        }

我在 filebytes1 上遇到异常,我想知道如何解决这个问题。我有一个名为 downloaddate 的参数,它是 HTML 中的日历日期选择器。每次我选择一个日期并单击按钮时,我都会收到异常 System.IO.IOException: 'the filename, directory name, or volume label syntax is wrong.我该如何解决这个问题?

【问题讨论】:

  • 你有没有在运行时检查过“path1 + filename”的值是什么?
  • DateTime 将包含 / 字符,这些字符对于文件名不安全。我希望您需要格式化日期以匹配文件名的实际名称。
  • @burcinsahin 是的,我做到了,并且路径与文件位置匹配。

标签: c# datetime exception filepath


【解决方案1】:

我创建了一个名为 Date 的变量并将其设置为 New DateTime。然后,引用变量 Date 来下载日期参数。

代码如下:

DateTime Date = new DateTime();
Date = (DateTime)downloaddate;

string path1 = AppDomain.CurrentDomain.BaseDirectory + "logs/";
string filename = "industrialcomm-payload-" + Date.ToString("yyyyMMdd") + ".log";
byte[] filebytes1 = System.IO.File.ReadAllBytes(path1 + filename);
string contentType1 = "text/plain";

return File(filebytes1, contentType1, filename);

这会将日历下载日期设置为 DateTime()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 2022-12-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2017-11-03
    • 2018-03-07
    相关资源
    最近更新 更多