Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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#保持文件打开,但覆盖内容_C#_File_Overwrite - Fatal编程技术网

c#保持文件打开,但覆盖内容

c#保持文件打开,但覆盖内容,c#,file,overwrite,C#,File,Overwrite,我有一个第三方应用程序,它定期从我的C#.Net应用程序读取输出。由于某些限制,我只能将输出写入一个文件,然后由第三方应用程序读取 我每次都需要覆盖同一文件的内容。 我目前正在用C#通过使用 第三方应用程序定期检查文件并读取内容。这可以很好地工作,但是CPU使用率很高。用文本编写器替换File.WriteAllText可以解决CPU使用率高的问题,但是我的文本会附加到文件中,而不是覆盖文件 有人能给我指出一个正确的方向吗?我可以用C#打开一个文件,并定期覆盖它的内容,而不会有太多开销 编辑:我通

我有一个第三方应用程序,它定期从我的C#.Net应用程序读取输出。
由于某些限制,我只能将输出写入一个文件,然后由第三方应用程序读取

我每次都需要覆盖同一文件的内容。
我目前正在用C#通过使用

第三方应用程序定期检查文件并读取内容。这可以很好地工作,但是CPU使用率很高。用文本编写器替换File.WriteAllText可以解决CPU使用率高的问题,但是我的文本会附加到文件中,而不是覆盖文件

有人能给我指出一个正确的方向吗?我可以用C#打开一个文件,并定期覆盖它的内容,而不会有太多开销


编辑:我通过选择每循环20次写入一次文件,而不是每循环一次,来修复CPU使用率。下面给出的所有答案都有效,但与关闭文件和重新打开相关的开销。谢谢

使用文本编写器,但在开始编写之前请清除文件内容。大概是这样的:

        string path = null;//path of file
        byte[] bytes_to_write = null;
        System.IO.File.WriteAllText(path, string.Empty);
        System.IO.FileStream str = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Write, System.IO.FileShare.Read);
        str.Write(bytes_to_write, 0, bytes_to_write.Length);

也许本例中的某些内容会有所帮助?

使用文本编写器,但在开始编写之前请清除文件的内容。大概是这样的:

        string path = null;//path of file
        byte[] bytes_to_write = null;
        System.IO.File.WriteAllText(path, string.Empty);
        System.IO.FileStream str = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Write, System.IO.FileShare.Read);
        str.Write(bytes_to_write, 0, bytes_to_write.Length);

也许这个例子中的一些东西会有所帮助?

使用
文件。使用
文件模式打开
截断
为您的
文本编写器创建文件流
使用
文件。使用
文件模式打开
截断
为您的
文本编写器创建文件流
<
false
作为构造函数的
append参数

TextWriter tsw = new StreamWriter(path, false);

Ref:

false
作为构造函数的
附加参数传递:

TextWriter tsw = new StreamWriter(path, false);

参考:

您是否尝试过使用Thread.Sleep


你试过使用Thread.Sleep吗

有人能给我指出一个正确的方向吗?我可以用C#打开一个文件,并定期覆盖它的内容,而不会有太多开销

下面是我在Silverlight 4中是如何做到这一点的。因为您没有使用Silverlight,所以您不会使用独立存储,但无论支持存储是什么,相同的技术都会起作用

有趣的一点是Write()方法:

从方法:

在派生类中重写时,设置当前流的长度

请确保使用AutoFlush(正如我在本例中所做的那样)或通过在
logWriter.Write()之后添加
logWriter.flush()
来刷新流

//
///表示独立存储中的日志文件。
/// 
公共静态类日志
{
private const string FileName=“TestLog.xml”;
私有静态隔离存储文件;
私有静态隔离存储文件流logWriterFileStream;
私有静态流编写器日志编写器;
公共静态XDocument Xml{get;private set;}
静态日志()
{
isoStore=IsolatedStorageFile.GetUserStoreForApplication();
logWriterFileStream=isoStore.OpenFile(
文件名,
FileMode.Create,
FileAccess.Write,
文件共享(无);
logWriter=新的StreamWriter(logWriterFileStream);
logWriter.AutoFlush=true;
Xml=新XDocument(新XElement(“测试”);
}
/// 
///将测试日志XML的快照写入隔离存储。
/// 
公共静态无效写入(XElement testContextElement)
{
Add(testContextElement);
logWriter.BaseStream.SetLength(0);
Write(Xml.ToString());
}
}
有人能给我指出一个正确的方向吗?我可以用C#打开一个文件,并定期覆盖它的内容,而不会有太多开销

下面是我在Silverlight 4中是如何做到这一点的。因为您没有使用Silverlight,所以您不会使用独立存储,但无论支持存储是什么,相同的技术都会起作用

有趣的一点是Write()方法:

从方法:

在派生类中重写时,设置当前流的长度

请确保使用AutoFlush(正如我在本例中所做的那样)或通过在
logWriter.Write()之后添加
logWriter.flush()
来刷新流

//
///表示独立存储中的日志文件。
/// 
公共静态类日志
{
private const string FileName=“TestLog.xml”;
私有静态隔离存储文件;
私有静态隔离存储文件流logWriterFileStream;
私有静态流编写器日志编写器;
公共静态XDocument Xml{get;private set;}
静态日志()
{
isoStore=IsolatedStorageFile.GetUserStoreForApplication();
logWriterFileStream=isoStore.OpenFile(
文件名,
FileMode.Create,
FileAccess.Write,
文件共享(无);
logWriter=新的StreamWriter(logWriterFileStream);
logWriter.AutoFlush=true;
Xml=新XDocument(新XElement(“测试”);
}
/// 
///将测试日志XML的快照写入隔离存储。
/// 
公共静态无效写入(XElement testContextElement)
{
Add(testContextElement);
logWriter.BaseStream.SetLength(0);
Write(Xml.ToString());
}
}

每次在我的循环中使用System.IO.file.WriteAllText(path,string.Empty)清除文件;也会导致相同的开销,因为我必须在循环中重复执行此操作;也会导致相同的开销,因为我必须在循环中重复执行。
/// <summary>
/// Represents a log file in isolated storage.
/// </summary>
public static class Log
{
    private const string FileName = "TestLog.xml";
    private static IsolatedStorageFile isoStore;
    private static IsolatedStorageFileStream logWriterFileStream;
    private static StreamWriter logWriter;

    public static XDocument Xml { get; private set; }

    static Log()
    {
        isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        logWriterFileStream = isoStore.OpenFile(
            FileName, 
            FileMode.Create, 
            FileAccess.Write, 
            FileShare.None);
        logWriter = new StreamWriter(logWriterFileStream);
        logWriter.AutoFlush = true;

        Xml = new XDocument(new XElement("Tests"));
    }

    /// <summary>
    /// Writes a snapshot of the test log XML to isolated storage.
    /// </summary>
    public static void Write(XElement testContextElement)
    {
        Xml.Root.Add(testContextElement);
        logWriter.BaseStream.SetLength(0);
        logWriter.Write(Xml.ToString());
    }
}