C# 通过文件共享锁定时读取、删除和写入文件。无?

C# 通过文件共享锁定时读取、删除和写入文件。无?,c#,c#-4.0,filestream,C#,C# 4.0,Filestream,这可能已经被问过了,所以我提前道歉。然而,我觉得这里有足够多的部分是我的问题所独有的,我没有发现这是值得提出这个问题的 我正在尝试使用FileStream方法打开文件。我需要从外部读/写锁定打开此文件,这就是我在打开文件时使用FileShare.None属性的原因。打开并锁定后,我将文件内容逐行读取到字符串数组中。我根据需要更新的信息更改其中一行,然后将这些行写回文件 FileStream fs = new FileStream("trains.txt", FileMode.Open, File

这可能已经被问过了,所以我提前道歉。然而,我觉得这里有足够多的部分是我的问题所独有的,我没有发现这是值得提出这个问题的

我正在尝试使用
FileStream
方法打开文件。我需要从外部读/写锁定打开此文件,这就是我在打开文件时使用
FileShare.None
属性的原因。打开并锁定后,我将文件内容逐行读取到字符串数组中。我根据需要更新的信息更改其中一行,然后将这些行写回文件

FileStream fs = new FileStream("trains.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);

string[] lines = new string[trains.Length];

//Loop through all lines in the file
for (int i = 0; i < lines.Length; i++)
{
    lines[i] = sr.ReadLine();

    /*If the ID of the train in the file matches the parameter, then
        set the line to the InfoLine of the train.*/
    if (lines[i][0].ToString() == train.ID().ToString())
    {
        lines[i] = train.GetInfoLine();
    }
}

//Write back the lines to the file
for (int i = 0; i < lines.Length; i++)
{
    sw.WriteLine(lines[i]);
}

sw.Close();
sr.Close();
fs.Close();
我遇到的问题是,写入的行被附加在读取的原始行之后。我需要擦除文件,一旦我从中读取,以便我写回的是文件中唯一的东西

FileStream fs = new FileStream("trains.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);

string[] lines = new string[trains.Length];

//Loop through all lines in the file
for (int i = 0; i < lines.Length; i++)
{
    lines[i] = sr.ReadLine();

    /*If the ID of the train in the file matches the parameter, then
        set the line to the InfoLine of the train.*/
    if (lines[i][0].ToString() == train.ID().ToString())
    {
        lines[i] = train.GetInfoLine();
    }
}

//Write back the lines to the file
for (int i = 0; i < lines.Length; i++)
{
    sw.WriteLine(lines[i]);
}

sw.Close();
sr.Close();
fs.Close();
我很困惑,因为我不想关闭
FileStream
,然后重新打开它,只进行写操作(这会清除文件),因为这样会释放我对文件的锁定

FileStream fs = new FileStream("trains.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);

string[] lines = new string[trains.Length];

//Loop through all lines in the file
for (int i = 0; i < lines.Length; i++)
{
    lines[i] = sr.ReadLine();

    /*If the ID of the train in the file matches the parameter, then
        set the line to the InfoLine of the train.*/
    if (lines[i][0].ToString() == train.ID().ToString())
    {
        lines[i] = train.GetInfoLine();
    }
}

//Write back the lines to the file
for (int i = 0; i < lines.Length; i++)
{
    sw.WriteLine(lines[i]);
}

sw.Close();
sr.Close();
fs.Close();
FileStream fs=newfilestream(“trains.txt”,FileMode.Open,FileAccess.ReadWrite,FileShare.None);
StreamReader sr=新的StreamReader(fs);
StreamWriter sw=新StreamWriter(fs);
字符串[]行=新字符串[trains.Length];
//循环浏览文件中的所有行
对于(int i=0;i
在上面的代码中,trains.Length只是类对象数组的长度
GetInfoLine()
是我的一个方法,它返回我要写入文件的信息字符串。

我将按照以下方式执行:

FileStream fs = new FileStream("trains.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);

string[] lines = new string[trains.Length];

//Loop through all lines in the file
for (int i = 0; i < lines.Length; i++)
{
    lines[i] = sr.ReadLine();

    /*If the ID of the train in the file matches the parameter, then
        set the line to the InfoLine of the train.*/
    if (lines[i][0].ToString() == train.ID().ToString())
    {
        lines[i] = train.GetInfoLine();
    }
}

//Write back the lines to the file
for (int i = 0; i < lines.Length; i++)
{
    sw.WriteLine(lines[i]);
}

sw.Close();
sr.Close();
fs.Close();
string line = "";
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);

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

while ((line = sr.ReadLine()) != null)
{
    line = "*" + line; //Do your processing
    lines.Add(line);
}

fs.SetLength(0);

foreach (var newline in lines)
{
    sw.WriteLine(newline);
}
sw.Flush();
fs.Close();
字符串行=”;
FileStream fs=newfilestream(文件名,FileMode.Open,FileAccess.ReadWrite,FileShare.None);
StreamReader sr=新的StreamReader(fs);
StreamWriter sw=新StreamWriter(fs);
列表行=新列表();
而((line=sr.ReadLine())!=null)
{
line=“*”+line;//进行处理
行。添加(行);
}
fs.SetLength(0);
foreach(行中的var换行符)
{
西南写线(新线);
}
sw.Flush();
fs.Close();

参见
fs.SetLength(0)

非常感谢您<代码>设置长度(0)工作得很有魅力!