Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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/4/regex/17.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# 使用StreamReader在文件中查找正则表达式,并使用StreamWriter覆盖它_C#_Regex_Streamreader_Streamwriter - Fatal编程技术网

C# 使用StreamReader在文件中查找正则表达式,并使用StreamWriter覆盖它

C# 使用StreamReader在文件中查找正则表达式,并使用StreamWriter覆盖它,c#,regex,streamreader,streamwriter,C#,Regex,Streamreader,Streamwriter,我正在使用StreamReader读取文本文件,并执行Regex.Match以查找特定信息,现在当我找到它时,我想将其替换为Regex.replace,并将此替换内容写回该文件 这是我的文件中的文本: /// /// <Command Name="Press_Button" Comment="Press button" Security="Security1"> /// /// <Command Name="Create_Button" Comment="Create

我正在使用StreamReader读取文本文件,并执行Regex.Match以查找特定信息,现在当我找到它时,我想将其替换为Regex.replace,并将此替换内容写回该文件

这是我的文件中的文本:

/// 
/// <Command Name="Press_Button"  Comment="Press button" Security="Security1">
/// 
/// <Command Name="Create_Button"  Comment="Create button" Security="Security3">
/// ... lots of other Commands 
//
/// 
/// 
/// 
/// ... 许多其他命令
现在我需要在Create_Button命令中找到:Security=“Security3”>将其更改为Security=“Security2”>并将其写回文件

do { 
    // read line by line 
    string ReadLine = InfoStreamReader.ReadLine();

    if (ReadLine.Contains("<Command Name"))
     {
         // now I need to find Security1, replace it with Security2 and write back to the file
     }
   }
while (!InfoStreamReader.EndOfStream);
do{
//逐行阅读
string ReadLine=InfoStreamReader.ReadLine();

if(ReadLine.Contains(“我会做更多类似的事情。您不能像在这里描述的那样直接写入文件中的一行

这不使用正则表达式,但实现了相同的功能

var fileContents = System.IO.File.ReadAllText(@"<File Path>");

fileContents = fileContents.Replace("Security1", "Security2"); 

System.IO.File.WriteAllText(@"<File Path>", fileContents);

来源:

我会做更多类似的事情。你不能像你描述的那样直接写入文件中的一行

这不使用正则表达式,但实现了相同的功能

var fileContents = System.IO.File.ReadAllText(@"<File Path>");

fileContents = fileContents.Replace("Security1", "Security2"); 

System.IO.File.WriteAllText(@"<File Path>", fileContents);

来源:

我有许多行使用不同的命令,首先我需要找到需要替换SecurityGroup的特定行,然后再编写它back@Stanislav就像我说的,你不能像那样简单地替换文件中的一行。你必须重新写入整个文件。嗯,我怎么能同时读取和写入文件,这样我的l行计数将保持正确。你不能这样做。我不知道我能说得清楚多少。我在这里展示的两种方法都不会影响你的行计数。@Stanislav我看到了你的编辑,这些示例不是你要找的吗?我有许多行使用不同的命令,首先我需要找到需要替换行的特定行然后才写它back@Stanislav就像我说的,你不能像那样简单地替换文件中的一行。你必须重新写入整个文件。嗯,我怎么能同时读取和写入文件,这样我的行数才能保持正确?你不能这样做。我不知道我能说得清楚多少。你的行数不会是aff我在这里展示了两种方法。@Stanislav我看到了你的编辑,这些例子不是你想要的吗?