Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每小时创建一个csv文件?_C#_.net - Fatal编程技术网

C# 每小时创建一个csv文件?

C# 每小时创建一个csv文件?,c#,.net,C#,.net,我必须每小时创建一个csv文件。我需要创建一个当天和当天的目录 在那天的同一个目录中,我必须每小时创建一个文件 public string CreateFile() { string rootDirectory = ConfigurationManager.AppSettings["BasePath"].ToString(); string directoryName = "tracker-" + DateTime.Now.ToString("MM-dd-yyyy");

我必须每小时创建一个csv文件。我需要创建一个当天和当天的目录 在那天的同一个目录中,我必须每小时创建一个文件

public string CreateFile()
{
    string rootDirectory = ConfigurationManager.AppSettings["BasePath"].ToString();
    string directoryName = "tracker-" + DateTime.Now.ToString("MM-dd-yyyy"); 
    string fileName = "tracker-" + DateTime.Now.ToString("MM-dd-yyyy") + ".csv"; //here i want the file to be for every hour
    string directoryPath=Path.Combine(rootDirectory, directoryName)
    string path = Path.Combine(directoryPath, filename);
    if (!Directory.Exists(directoryName))
    {
          Directory.CreateDirectory(directoryName);
    }

    if (!System.IO.File.Exists(path))
    {
        System.IO.File.Create(path).Dispose();
        using (TextWriter tw = new StreamWriter(path))
        {
            tw.WriteLine("New file created");
            tw.Close();
        }
    }
    return path;
}

这里有谁能帮我一下吗。

如果你的意思是你需要一个应用程序每小时创建一个
csv
文件,那么你可以使用
计时器

Timer _tmrCsv = new Timer { Interval = 3600000 };
private void Form2_Load(object sender, EventArgs e)
{
    _tmrCsv.Tick += _tmrCsv_Tick;
    _tmrCsv.Enabled = true;

}

void _tmrCsv_Tick(object sender, EventArgs e)
{
    CreateFile();
}
你可以用一个

下面是一个示例代码:

 private static System.Timers.Timer aTimer;
 private static int counter = 0;
 private static void SetTimer()
{
    // Create a timer with a one minute interval.
    aTimer = new System.Timers.Timer(60000);
    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += OnTimedEvent;
    aTimer.AutoReset = true;
    aTimer.Enabled = true;
}

  private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    //Increment the counter
     counter++;

     if (counter== 60)
     {
        // One hour has passed
        // Reset the counter and create your file
        counter = 0;
        CreateFile();
     }
}

使用windows任务计划程序。嗨,尝试将小时、分钟和秒组件放入自定义日期时间格式:string fileName=“tracker-”+DateTime.Now.ToString(“HH:mm:ss mm dd yyyy”)+“.csv”//在这里,我希望每小时都有一份文件,希望我能帮上忙!您的问题是“如何每小时运行一个可执行文件”,还是“如何将小时添加到文件名”?这两个问题以前都已回答过,请尝试搜索@George的解决方案不适用于后者,因为文件名中不能有冒号(
)。不要理会那些说使用定时器的人,那是非常脆弱的@Tim的解决方案是正确的。如果你不能使用Tim的答案,那么在类库项目中创建你的代码(如果你还没有),然后将windows服务项目添加到解决方案中,并将其放置在windows服务中。使用
Quartz
库(最好与windows服务结合使用)如何获取当前小时数then@SoftwareNerdDateTime.Now.hour为什么不使用间隔为1小时的计时器?您的示例代码将每60小时创建一个文件!