【发布时间】:2020-07-02 17:06:51
【问题描述】:
在配置文件/etc/ssh/sshd_config我想删除从Match user sftpuser这一行开始的4行。我已经开发了下面的代码,问题是函数SFTPCLient.WriteAllLines(string path, IEnumerable<string> contents);不会覆盖所有现有内容,但是如果新内容比旧内容长,它会用新的一次替换相同的字节,然后其余的。
SftpClient sftpClient = new SftpClient("xx.xxx.xxx.xxx", "root", "************");
sftpClient.Connect();
var newLines = new List<string>();
string[] oldlines = sftpClient.ReadAllLines("/sftp/sftpuser/test.txt");
int index = 0;
foreach(string line in oldlines)
{
if (line.Equals("Match user sftpuser"))
{
break;
}
index++;
}
int[] indexdelete = { index, index + 1, index + 2, index + 3 };
for(int i =0; i < oldlines.Length; i++)
{
if (indexdelete.Contains(i)) continue;
newLines.Add(oldlines[i]);
}
sftpClient.WriteAllLines("/sftp/sftpuser/test.txt", newLines);
sftpClient.Disconnect();
【问题讨论】: