相关疑难解决方法(0)

如何在C#中读写文件

我想要读取和写入文件.这不起作用.

static void Main(string[] args)
{
    StreamReader sr = new StreamReader(@"C:\words.txt");
    StreamWriter sw = new StreamWriter(@"C:\words.txt");
}
Run Code Online (Sandbox Code Playgroud)

我如何在C#中读取和写入文件?

c# file-io filestream

66
推荐指数
3
解决办法
16万
查看次数

c#连续读取文件

我想像GNU尾部一样连续读取文件"-f"param.我需要它来实时读取日志文件.做正确的方法是什么?

c# file-io

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

读取和写入文件C#时共享违规IOException

这是我的代码:

public static TextWriter twLog = null;
private int fileNo = 1;
private string line = null;

TextReader tr = new StreamReader("file_no.txt");
TextWriter tw = new StreamWriter("file_no.txt");
line = tr.ReadLine();
if(line != null){
    fileNo = int.Parse(line);
    twLog = new StreamWriter("log_" + line + ".txt");
}else{
    twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");  
}
System.IO.File.WriteAllText("file_no.txt",string.Empty);
tw.WriteLine((fileNo++).ToString());
tr.Close();
tw.Close();
twLog.Close();
Run Code Online (Sandbox Code Playgroud)

它抛出此错误:

IOException:路径C:\ Users\Water Simulation\file_no.txt上的共享冲突

我要做的只是打开一个带有log_x.txt名称的文件,并从file_no.txt文件中取出"x".如果file_no.txt文件为空,请将日志文件的名称设为log_1.txt并写入"fileNo + 1" to file_no.txt.新程序启动后,新的日志文件名必须是log_2.txt.But我收到此错误,我无法理解我做错了什么.谢谢你的帮助.

c# file-io textreader textwriter

21
推荐指数
2
解决办法
7万
查看次数

System.IO.IOException:'该进程无法访问文件'@ .txt',因为它正在被另一个进程使用。

我是编程新手,但有一个问题。如果我有两个功能,一个创建一个文本文件并将其写入,另一个打开同一个文本文件并从中读取。

我得到的错误是:

System.IO.IOException:'该进程无法访问文件'@ .txt',因为它正在被另一个进程使用。

我尝试为每个功能设置单独的计时器,但仍然无法正常工作。我认为最好的方法是,直到功能一结束,功能二才开始。

你能帮我实现这个目标吗?非常感谢你!麦克风

源代码:

public Form1() {
    InitializeComponent();
    System.Timers.Timer timerButtona1 = new System.Timers.Timer();
    timerButtona1.Elapsed += new ElapsedEventHandler(tickTimera1);
    timerButtona1.Interval = 3003;
    timerButtona1.Enabled = true;
}

private async void tickTimera1(object source, ElapsedEventArgs e) {
    function1();
    function2();
}

void function1() {
    List<string> linki = new List<string>();

    linki.Add("https://link1.net/");
    linki.Add("https://link2.net/");
    linki.Add("https://link3.net/");

    List<string> fileNames = new List<string>();

    fileNames.Add("name1");
    fileNames.Add("name2");
    fileNames.Add("name3");

    for (int x = 0; x < fileNames.Count; x++) {
        GET(linki[x], fileNames[x]);
        //System.Threading.Thread.Sleep(6000);
    }
}

async void GET(string link, string fileName) {
    var …
Run Code Online (Sandbox Code Playgroud)

c# concurrency ioexception

6
推荐指数
4
解决办法
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万
查看次数