我有一个文本文件,其中包含一些我想要编辑的信息。该文件看起来是这样的:C#更改文件中的特定行
id: 31
name: Anna
profession: Doctor
我可以读一个StreamReader
该条目,并在我的应用程序出示。然后我希望用户能够更改条目的name
和profession
,所以我想将这些特定行编辑为新值,同时保持id
完整(在我的真实代码中,不仅有几行,但很多行只有一些应该改变)。因此,例如,我希望在我的操作结束时将文件看起来像这样。
id: 31
name: Emma
profession: Programmer
但是,我也必须考虑到,有时候行并不是事先存在的。例如,编辑Anna
到Emma
之前,它并不一定是她有一个行业,该文件可能是这个样子的:
id: 31
name: Anna
而且在这种情况下,我想行profession: Programmer
添加到结束。
我尝试使用FileStream
与ReadWrite
访问,我给一个StreamReader
和StreamWriter
,但后来我发现没有更改或替换文本行,只有阅读它,写一个相同的新线,同时保持的方式旧。
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
using (StreamReader reader = new StreamReader(fileStream))
using (StreamWriter writer = new StreamWriter(fileStream))
{
bool idExists = false;
bool nameExists = false;
bool tagsExist = false;
string line;
while((line = reader.ReadLine()) != null)
{
if (line.StartsWith("id:"))
idExists = true;
else if (line.StartsWith("name:"))
{
nameExists = true;
line = $"name: {entryToSave.Name}";
writer.WriteLine(line); // Will write an additional line and not replace
}
else if (line.StartsWith("profession:"))
{
professionExists = true;
line = $"profession: {entryToSave.Profession}";
writer.WriteLine(line); // Will write an additional line and not replace
}
}
if (!idExists)
writer.WriteLine($"id: {generatedId}");
if (!nameExists)
writer.WriteLine($"name: {entryToSave.Name}");
if (!professionExists)
writer.WriteLine($"profession: {entryToSave.Profession}");
}
我也尝试过使用File.ReadAllLines
,遍历行,然后写回所有的线到该文件,仅修改将被修改的行。但是,由于某些原因我无法通过File.WriteAllLines
访问该文件,因为StreamWriter
可以访问。代码:
var previousData = File.ReadAllLines(filePath);
var newData = new List<string>();
bool idExists = false;
bool nameExists = false;
bool professionExists = false;
for (int i = 0; i < previousData.Length; i++)
{
var line = previousData[i];
if (line.StartsWith("id:")
idExists = true;
else if (line.StartsWith("name:")
{
nameExists = true;
line = $"name: {entryToSave.Name}";
}
else if (line.StartsWith("profession:"))
{
professionExists = true;
line = $"profession: {entryToSave.Profession}";
}
newData.Add(line);
}
if (!idExists)
newData.Add($"id: {generatedId}");
if (!nameExists)
newData.Add($"name: {entryToSave.Name}");
if (!professionExists)
newData.Add($"profession: {entryToSave.Profession}");
File.WriteAllLines(filePath, newData.ToArray()); // Access denied
这是如何最容易实现的,没有文件流互相干扰?
我刚刚发现我自己现在我无法做File.WriteAllLines的原因不是文件很忙,因为某种原因访问被拒绝,我不知道如何解决。我会编辑我的问题。 – Helena
如果您发现我的解决方案有帮助,我会非常感激,如果您将其标记为答案:) – CitiZen
哦,对,对不起,我忘了!会做。 – Helena