Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the configuration file /etc/ssh/sshd_config I want to delete 4 lines starting from this line Match user sftpuser. I have developed the code below, The problem is the function SFTPCLient.WriteAllLines(string path, IEnumerable<string> contents): does not overwrite all the existing content, but if the new content is longer than the old, it substitutes the same bytes with the new once following by the rest.
Are there any alternative for that?

What I have tried:

<pre lang="c#">
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();
Posted
Updated 30-Jun-20 0:42am
v4
Comments
Garth J Lancaster 30-Jun-20 6:22am    
1. please update your question with which SFTP Client you are using (use "Improve question")
2. have you verified - eg using Console.WriteLine() and/or a debugger that newLines has the correct content ? (ie, missing the 4 lines from []indexdelete) ?
3. have you checked the permissions on the directory/file ?
4. have you tried deleting the file before writing the new lines to it (since I cant tell what sftpClient.WriteAllLines is doing) ... try a System.IO.File.Delete("/sftp/sftpuser/test.txt") before the sftpClient.WriteAllLines ...
Fadwa DHIFI 30-Jun-20 6:38am    
Hello, 1. I didn't quit understand this remark, but am using SSH.NET
2.I am sure about the content of newLines (I have used Console.WriteLine())
3. Yes, Actually, It writes into it, but not overwrites it all: sftpClient.WriteAllLines does not overwrite all the existing content, but if the new content is longer than the old, it substitutes the same bytes with the new once following by the rest.
4. I don't want to risque deleting this file: It may prevent me form ssh again.
Garth J Lancaster 30-Jun-20 6:57am    
From what I have seen of discussions on 'WriteAllLines', it will write to a file but not truncate it first (I havent yet found the exact code for the library) ..

That being the case, can you write to eg /sftp/sftpuser/test.txt.new", if everything else you're doing is successful, delete the original file and rename .new to the original file name ?
Richard MacCutchan 30-Jun-20 6:47am    
Which SftpClient are you using? Make sure that it rewrites the file completely on the WriteAllLines method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900