相关疑难解决方法(0)

在C#中同时读写文件

我有一个文件包含我想要监视更改的数据,以及添加我自己的更改.想像"Tail -f foo.txt".

基于这个线程,看起来我应该创建一个文件流,并将它传递给一个编写器和读者.但是,当读者到达原始文件的末尾时,它无法看到我自己写的更新.

我知道这似乎是一个奇怪的情况......它更多的是一个实验,看看它是否可以完成.

这是我尝试的示例案例:


foo.txt:
a
b
c
d
e
f


        string test = "foo.txt";
        System.IO.FileStream fs = new System.IO.FileStream(test, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);

        var sw = new System.IO.StreamWriter(fs);
        var sr = new System.IO.StreamReader(fs);

        var res = sr.ReadLine();
        res = sr.ReadLine();
        sw.WriteLine("g");
        sw.Flush();
        res = sr.ReadLine();
        res = sr.ReadLine();
        sw.WriteLine("h");
        sw.Flush();
        sw.WriteLine("i");
        sw.Flush();
        sw.WriteLine("j");
        sw.Flush();
        sw.WriteLine("k");
        sw.Flush();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
Run Code Online (Sandbox Code Playgroud)

过了"f"后,读者返回null.

c# filestream streamwriter streamreader

14
推荐指数
2
解决办法
4万
查看次数

System.IO.WriteAllBytes - 访问路径拒绝错误

目前正在Windows 7上的Visual Studio 2010 .NET 4中开发C#WinForms应用程序.

首先,我使用File.ReadAllBytes()方法从文件中读取字节流.然后,当尝试写回文件时,我在使用WriteAllBytes方法时获得了路径拒绝错误的访问权限.

我试过传入文字路径,Environment.SpecialFolder.ApplicationData,Path.GetTempPath(),但都给我提供了同样的错误.

我检查了这些文件夹的权限,并尝试以管理员模式启动程序,没有运气.

c# winforms

8
推荐指数
2
解决办法
3万
查看次数

在同一流中读取和写入文件

我正在尝试以某种方式读取和写入同一文件,以便其他程序无法访问其间的文件:

  FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);               
  StreamReader sr = new StreamReader(fs);
  StreamWriter sw = new StreamWriter(fs);
  newString = sr.ReadToEnd() + "somethingNew";
  sw.Write(newString);
  fs.Close();
Run Code Online (Sandbox Code Playgroud)

该文件永远不会被写入。如果我调试,我可以看到读取器设法获取文件的内容,但写入器似乎无法写入文件。什么都没发生。

我一直在看这个问题,似乎和我的一样。但是我无法让它工作。

c#

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×3

filestream ×1

streamreader ×1

streamwriter ×1

winforms ×1