简体   繁体   English

在应用程序完成运行之前,不会写入C#文件

[英]C# file not being written to until the application finishes running

I've got a set of Specflow tests that I'm running and I'm trying to write the results so far to a file after each scenario finishes, and then I write again at the end of the entire test run. 我有一组我正在运行的Specflow测试,我试图在每个场景完成后将结果写到文件中,然后在整个测试运行结束时再次写入。 If I run the tests in debug mode then this code is hit after each scenario, but the file only appears in Windows Explorer after all the tests finish (or I force the tests to stop). 如果我在调试模式下运行测试,则在每个场景之后都会触发此代码,但该文件仅在所有测试完成后显示在Windows资源管理器中(或者我强制测试停止)。 The code below, that writes to the file is in a separate project to the Specflow test project. 写入文件的下面的代码位于Specflow测试项目的单独项目中。

I originally was not flushing and had this in place of StreamWriter: 我最初并没有冲洗,而是用StreamWriter取代了:

using (var file = new FileInfo(filePath).AppendText())

But that wasn't working, so after looking up various examples on the internet I added extra Flush, Close, then Process.Start, changed to StreamWriter and none of them helped. 但这不起作用,所以在互联网上查找各种示例后,我添加了额外的Flush,Close,然后Process.Start,更改为StreamWriter,但没有一个帮助。 The current code that still isn't working is: 目前仍然无效的代码是:

    private string rootFolderPath = Directory.GetParent(@"..\..\..\..") +@"\";
    public void WriteAllTestScenarioNames(List<ScenarioResult> results, string fileName, string directoryName)
    {
        results.Sort();
        Directory.CreateDirectory(rootFolderPath + directoryName);
        string filePath = rootFolderPath + directoryName + @"\" + fileName;
        using (var file = new System.IO.StreamWriter(filePath))
        {
            file.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            foreach (var scenarioResult in results)
            {
                file.WriteLine(scenarioResult.ToString());
            }
            file.Flush();//flush the file to ensure that it's written now
            file.Close();//flush the file to ensure that it's written now
        }
        Process.Start(filePath);    //flush the file to ensure that it's written now
    }

In the example I'm trying there are only 3 lines in the output file when it finally finishes and the Created, Modified and Accessed dates in the file properties in Windows Explorer are identical. 在我尝试的示例中,当输出文件最终完成并且Windows资源管理器中的文件属性中的Created,Modified和Accessed日期相同时,输出文件中只有3行。

I am using Windows 7 Enterprise SP1 64-bit with .NET Framework 4.5.2 我正在使用Windows 7 Enterprise SP1 64位与.NET Framework 4.5.2

It seems that the file is being written, the issue is that it wasn't showing up in Windows Explorer. 似乎正在编写文件,问题是它没有出现在Windows资源管理器中。 I stepped through again in debug mode, checked in Explorer, the file wasn't there. 我在调试模式下再次进入,在资源管理器中检查,文件不在那里。 I copied the file path from the variable's value and opened that exact path in Notepad++ and the file was there. 我从变量的值复制了文件路径,并在Notepad ++中打开了那个确切的路径,文件就在那里。 Following that the file now is showing in Explorer and updating after each test scenario finishes. 之后,该文件现在显示在资源管理器中,并在每个测试场景完成后进行更新。

Edit: It seems that the issue was 编辑:似乎问题是

private string rootFolderPath = Directory.GetParent(@"..\..\..\..") +@"\";

was returning a different result after each scenario to what it was after the test run. 在每个场景之后返回不同的结果到测试运行之后的结果。 One was in the root of my user folder (correct) and the other 3 directories above the Specflow project folder. 一个位于我的用户文件夹(正确)的根目录中,另一个位于Specflow项目文件夹上方的其他3个目录中。 In the first folder it's only outputting at the end, but in the second it's updating after every scenario. 在第一个文件夹中,它只在最后输出,但在第二个文件夹中,它会在每个场景后更新。

Try this: 尝试这个:

public void WriteAllTestScenarioNames(List<ScenarioResult> results, string fileName, string directoryName)
{
    results.Sort();
    Directory.CreateDirectory(rootFolderPath + directoryName);
    string filePath = rootFolderPath + directoryName + @"\" + fileName;
    FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(fs))
    {
        file.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
        foreach (var scenarioResult in results)
        {
            file.WriteLine(scenarioResult.ToString());
        }
    }
    fs.Close();

}

Also check if the filePath is getting formed properly and whether you have permission to write in that area - I suspect it may not be getting formed properly. 还要检查filePath是否正确形成以及您是否有权在该区域内写入 - 我怀疑它可能没有正确形成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#复制正在写入的文件 - C# Copy a File That is Being Written To 用C#编写文件时如何复制文件 - How to copy a file as it is being written in C# C# 刷新文件以获取另一个应用程序正在写入的文件的正确 lastwritetime - C# Refresh File to get the correct lastwritetime for file which is being written by another application 为什么我的c #Windows服务在没有任何消息写入应用程序事件日志的情况下停止运行? - Why does my c# Windows service stop running without any messages being written to the application event log? C#Cookie未写入 - c# cookies not being written to 如何在C#中按分数排序正在写入文本文件的数据 - how to order the data being written to a text file by score in c# c#检测文件是否已完成写入 - c# Detect if file has finished being written c#如何发送仍在写入的文件流并继续发送,直到创建结束 - c# How to send a filestream still being written and keep sending until the end of the creation 等待网页完成加载的C#代码 - C# code to wait until webpage finishes loading C#:读取子流程stdout块,直到另一个子流程完成? - C#: Read on subprocess stdout blocks until ANOTHER subprocess finishes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM