C
C#6mo ago
Ares

Cant navigate to different directories in sftp server using SSH.NET

I am trying to make code that will navigate throughout my sftp server using SSH.NET library, and I'm having trouble with the navigation. They have a built in way of changing the current working directory with client.ChangeDirectory, and you can access the directory with client.WorkingDirectory. However, this doesn't seem to do anything at all when I use it. Even though I change the workign directory, and I see the string change, when I do "pwd" in ssh to access the current directory, it is always the root no matter what. The second method to navigate would be to just use cd in ssh, using cSSH.RunCommand("cd ..."). However, this has the same issue where it doesn't seem to change the directory at all. Not sure why, but none of these methods work. I've been trying for a long time now but cant seem to figure out why. Thanks.
14 Replies
Ares
AresOP6mo ago
public async Task GetInformation(string url)
{
//Start Connections
SshClient cSSH = new SshClient(sftpConfig.Host, sftpConfig.Port, sftpConfig.UserName, sftpConfig.Password);
SftpClient client = new SftpClient(sftpConfig.Host, sftpConfig.Port, sftpConfig.UserName, sftpConfig.Password);

cSSH.Connect();
client.Connect();

SshCommand a = cSSH.RunCommand("snap install dotnet-sdk --classic");

//Clone repo
SshCommand x = cSSH.RunCommand($"git clone {url}");

//Navigate to the repo
SshCommand getSSHWorkingDirectory = cSSH.RunCommand("pwd");
string workingDirectory = getSSHWorkingDirectory.Result.Trim() + "/di-visualizer/DIVisualizer.Server";
client.ChangeDirectory(workingDirectory);

//Navigate to the repo second method
string cmdstr = "cd di-visualizer/DIVisualizer.Server";
var cmd = cSSH.RunCommand(cmdstr);


//Roslyn analysis
MSBuildLocator.RegisterDefaults();


var workspace = MSBuildWorkspace.Create();
string analysis = "dotnet run -r roslyn";
SshCommand b = cSSH.RunCommand(analysis);

var solution = await workspace.OpenSolutionAsync(client.WorkingDirectory);

foreach (var project in solution.Projects)
{
Console.WriteLine(project.AssemblyName);
}

//Close connections
client.ChangeDirectory("/root");
cSSH.RunCommand("rm -r di-visualizer");
client.Disconnect();
cSSH.Disconnect();
}
public async Task GetInformation(string url)
{
//Start Connections
SshClient cSSH = new SshClient(sftpConfig.Host, sftpConfig.Port, sftpConfig.UserName, sftpConfig.Password);
SftpClient client = new SftpClient(sftpConfig.Host, sftpConfig.Port, sftpConfig.UserName, sftpConfig.Password);

cSSH.Connect();
client.Connect();

SshCommand a = cSSH.RunCommand("snap install dotnet-sdk --classic");

//Clone repo
SshCommand x = cSSH.RunCommand($"git clone {url}");

//Navigate to the repo
SshCommand getSSHWorkingDirectory = cSSH.RunCommand("pwd");
string workingDirectory = getSSHWorkingDirectory.Result.Trim() + "/di-visualizer/DIVisualizer.Server";
client.ChangeDirectory(workingDirectory);

//Navigate to the repo second method
string cmdstr = "cd di-visualizer/DIVisualizer.Server";
var cmd = cSSH.RunCommand(cmdstr);


//Roslyn analysis
MSBuildLocator.RegisterDefaults();


var workspace = MSBuildWorkspace.Create();
string analysis = "dotnet run -r roslyn";
SshCommand b = cSSH.RunCommand(analysis);

var solution = await workspace.OpenSolutionAsync(client.WorkingDirectory);

foreach (var project in solution.Projects)
{
Console.WriteLine(project.AssemblyName);
}

//Close connections
client.ChangeDirectory("/root");
cSSH.RunCommand("rm -r di-visualizer");
client.Disconnect();
cSSH.Disconnect();
}
and just to be clear, this problem is only happening in my C# code, when i connect using my terminal this does not happen
Omnissiah
Omnissiah6mo ago
curious to me this works
using var ff = new SftpClient("192.168.2.50", "root", "...");
ff.Connect();
Console.WriteLine(ff.WorkingDirectory);
ff.ChangeDirectory("/etc");
Console.WriteLine(ff.WorkingDirectory);
ff.Disconnect();
using var ff = new SftpClient("192.168.2.50", "root", "...");
ff.Connect();
Console.WriteLine(ff.WorkingDirectory);
ff.ChangeDirectory("/etc");
Console.WriteLine(ff.WorkingDirectory);
ff.Disconnect();
maybe you can compare with another tool, even if only using plink/sftp by Process.Start
Ares
AresOP6mo ago
i can change the working directory just fine, cause in the yellow i am not at root anymore. but the problem is that when i do dotnet run -r roslyn, it says there is no csproj in the directory
No description
Ares
AresOP6mo ago
No description
Ares
AresOP6mo ago
which is not true because, in that directory, there is a .csproj
Ares
AresOP6mo ago
No description
Omnissiah
Omnissiah6mo ago
hmmm strange; did you try also setting explicitely the --project path?
Ares
AresOP6mo ago
yea i tried dotnet run -r roslyn --project DIVisualizer.Server.csproj and still the same project cant be found error
Omnissiah
Omnissiah6mo ago
no i mean dotnet ... --project /root/di-visualizer/DIVisualizer.Server
Ares
AresOP6mo ago
no ill try that ok thanks that worked, i did, dotnet run -r roslyn --project /root/di-visualizer/DIVisualizer.Server/DIVisualizer.Server.csproj
Omnissiah
Omnissiah6mo ago
it remains to understand why WorkingDirectory doesn't do what it's supposed to do
Ares
AresOP6mo ago
oh ok ill look into it some more tomorrow, to be honest i dont even think i want to run roslyn like that, i was just trying stuff out
Ares
AresOP6mo ago
Roslynator | My Docs
Roslynator is a set of code analysis tools for C#, powered by Roslyn.
Ares
AresOP6mo ago
i found this and i was gonna look through it to see what i actually want to do
Want results from more Discord servers?
Add your server