-6

Using SSH.Net I would like to do the following: Make a command to check if the file exists on a Linux Machine via C# and various ssh packages and/or winscp libraries and if the file or files do not exist on the linux machine, then it will create them, I enjoy the educational aspects of programming, so I am improving ones self, I apologise for not fully writing out the question.

private void TestCommmand(string ip, int port, string uname, string pw)
{
    // [1]
    SshClient cSSH = new SshClient(ip, port, uname, pw);
    cSSH.Connect();
    SshCommand x = new SshCommand();
    // [2] here is where the Check needs to happen
    //
    if (condition == true)
    {
        x = cSSH.RunCommand(" mkdir /var/www/html/app/cs");
    }
    else // if condition is false
    {
        cSSH.Disconnect();
        cSSH.Dispose();
    }
    //
}
Cœur
  • 32,421
  • 21
  • 173
  • 232
MasterCassidy
  • 39
  • 1
  • 11
  • So what's your question? It sounds like you just want us to write the code for you. Which part are you stuck at? – mason Feb 11 '16 at 03:56
  • thats kinda the whole point of asking right? im only asking how to check if a folder exists, and if it doenst, run mkdir. thats it. – MasterCassidy Feb 11 '16 at 03:58
  • See my answer here: http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net It applies to checking on a linux system via ssh as much as anywhere else. Just call `mkdir`, [maybe with the `-p` argument added](http://stackoverflow.com/questions/793858/how-to-mkdir-only-if-a-dir-does-not-already-exist) – Joel Coehoorn Feb 11 '16 at 04:01
  • 1
    Seems like the command has a `.Result` property that you could inspect. I've never used the library before, but it seems like a little research on your part would go a long ways here. – mason Feb 11 '16 at 04:04
  • So could someone provide me an example of a check of such, sorry i have A.D.D so reading documentation is a difficult task for folk like me. i have to dive in the deep end. – MasterCassidy Feb 11 '16 at 04:12
  • 1
    Here this can shine some light for you http://www.dotnetperls.com/file-exists – EasyE Feb 11 '16 at 04:38

2 Answers2

1

You can do mkdir -p /var/www/html/app/cs to create the directory and its parent if it doesn't exist.

The command will succeed as long as you've got permissions to create the first directory that doesn't exist. If only /var/www exists, it will create html, then app, then cs. If /var/www/html/app/cs exists, it will do nothing.

So your code would just be:

private void TestCommmand(string ip, int port, string uname, string pw)
{
    SshClient cSSH = new SshClient(ip, port, uname, pw);
    cSSH.Connect();
    SshCommand x = new SshCommand();
    x = cSSH.RunCommand("mkdir -p /var/www/html/app/cs");
    /// .. Continue with your work ...
}

Note that you should check errors after nearly each step: SSH connection can fail, command can fail because of permissions, etc ...

Colin Pitrat
  • 1,739
  • 1
  • 13
  • 27
1

This is something I ran into not long ago so trust me on here...

Let's check out this little guy below:

if(condition == true) 

In a way you're already answering your question, just not comprehending the syntax. (check it out)

using System;
using System.IO;

namespace StackOverflowQA
{
    class Program
     {

         static void Main(string[] args)
         {
            if(File.Exists(@"C:\Exercise\Derp\"))
             {
               DoWork();
             }
             else
             {
               Console.Out.Writelne("Wrong kid died");
               Console.Write("Enter a key to exit");
               Console.Read();
             }

            private static void DoWork()
            {

              //-- if you get to this point then check out that,
             //    NuGet pkg you're working with (WinSCP) and proceed! :) 
            }
         }

      }
 }

Before even getting into a third-party assembly or library/whatever, I think you need to first create a simple console application to find a file on your machine.

Create the folder I just described in-code and "DoWork();"

Cheers ^_^

Abraxas
  • 374
  • 3
  • 10
  • That's the thing i needed! Thanks for taking the time to put up the correct syntax for me, ill accept this as the answer, as this is what i was seeking, i knew what i needed to do, just couldnt think of the syntax, so cheers for that. – MasterCassidy May 09 '16 at 23:07
  • No problem my friend :) – Abraxas May 10 '16 at 17:23