【问题标题】:Creating a new .txt file with date in front, C#创建一个新的.txt文件,前面有日期,C#
【发布时间】:2009-06-02 18:28:29
【问题描述】:

我正在尝试从以下代码中获取以下内容:[今天的日期]___[文本文件名].txt:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication29
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteToFile();

        }

        static void WriteToFile()
        {

            StreamWriter sw;
            sw = File.CreateText("c:\\testtext.txt");
            sw.WriteLine("this is just a test");
            sw.Close();
            Console.WriteLine("File created successfully");



        }
    }
}

我尝试输入DateTime.Now.ToString(),但我无法合并字符串。

有人可以帮助我吗?我想要我正在创建的新文本文件的标题前面的日期。

【问题讨论】:

    标签: c# datetime text-files


    【解决方案1】:
    static void WriteToFile(string directory, string name)
    {
        string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now, name);
        string path = Path.Combine(directory, filename);
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.WriteLine("This is just a test");
        }
    }
    

    打电话:

    WriteToFile(@"C:\mydirectory", "myfilename");
    

    注意几点:

    • 使用自定义格式字符串指定日期,避免在 NTFS 中使用非法字符。
    • 前缀包含带有“@”字符串文字标记的路径的字符串,因此您不必转义路径中的反斜杠。
    • 使用 Path.Combine() 组合路径部分,避免使用路径分隔符。
    • 创建 StreamWriter 时使用 using 块;退出区块将处理 StreamWriter,并自动为您关闭文件。

    【讨论】:

    • 希望我能在这方面超过 +1。您出色的笔记给出了答案背后的答案。
    • 不需要 StreamWriter: File.WriteAllText(path, "This is just a test");
    • @John:我试图在保留 OP 的原始内容和结构与建议最佳实践之间取得平衡。但是,是的,绝对是,如果你只写一行的话。
    • 只是个人喜好,但我会将格式化字符串放在原始字符串中: string.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now,名称);
    【解决方案2】:

    您希望在 DateTime.Now 上执行自定义字符串格式。您可以使用 String.Format() 将结果与您的基本文件名结合起来。

    要附加到文件名的路径上,请使用 Path.Combine()。

    最后,使用 using() 块在完成后正确关闭和处置您的 StreamWriter...

    string myFileName = String.Format("{0}__{1}", DateTime.Now.ToString("yyyyMMddhhnnss"), "MyFileName");
    strign myFullPath = Path.Combine("C:\\Documents and Settings\\bob.jones\\Desktop", myFileName)
    using (StreamWriter sw = File.CreateText(myFullPath))
    {
        sw.WriteLine("this is just a test");
    }
    
    Console.WriteLine("File created successfully");
    

    编辑:修复了“C:\Documents and Settings\bob.jones\Desktop”路径的示例

    【讨论】:

    • string myFileName = String.Format("{0}__{1}", DateTime.Now.ToString("yyyyMMddhhnnss"), "MyFileName"); StreamWriter sw = File.CreateText(myFileName);
    • 这在大多数情况下都有效...但是我如何指定应该在其中创建此 .txt 的确切路径?例如,我希望在 C:\\Documents and Settings\\bob.jones\\Desktop 上创建这个 .txt 文件,我该如何指定?
    • +1 用于通过格式化去除文件名中可能非法的非数字字符。
    【解决方案3】:

    试试这个:

    string fileTitle = "testtext.txt";
    string fileDirectory = "C:\\Documents and Settings\username\My Documents\";
    File.CreateText(fileDirectory + DateTime.Now.ToString("ddMMYYYY") + fileTitle);
    

    ?

    【讨论】:

    • 请记住,日期时间可以在路径中包含非法字符,例如正斜杠 / 某些文化将其用作分隔符。
    【解决方案4】:

    要回答您对@Scott Ivey's answer 的评论中的问题: 要指定文件的写入位置,请在调用 CreateText() 之前或中添加所需的文件名路径。

    例如:

    String path = new String (@"C:\Documents and Settings\bob.jones\Desktop\");
    StreamWriter sw = File.CreateText(path + myFileName);
    

    String fullFilePath = new String (@"C:\Documents and Settings\bob.jones\Desktop\");
    fullFilePath += myFileName;
    StreamWriter sw = File.CreateText(fullFilePath);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多