Skip to main content

Remote server connection by SSH using C#

SSH is a secure network protocol to use remote shell services or execute commands. You can connect to a remote server by SSH on Visual Studio. To do this, you need a SSH library. My suggestion is SharpSSH.

Download DLL files from SourceForge. You should add these DLL files to your Visual Studio project:

  • DiffieHellman.dll
  • Org.Mentalis.Security.dll
  • Tamir.SharpSSH.dll

To connect to server and execute commands, code is here:

SshExec shell = new SshExec(hostIP, username);
shell.Password = password;
shell.Connect();
string output = shell.RunCommand(command);
shell.Close();

My SSH application is below. It is a simple SSH Command Prompt.

SSH Command Prompt
SSH Command Prompt

Creating PDF, Word, Excel, Outlook and Text files using C#

When you develop an application that needs exporting capability for several type of files, you should use some internal or external programming libraries. I will mention a few useful and free libraries for our exporting purposes.

Data Export

I don’t want to give much explanation about these libraries because you can find out easily If you want. My purpose is giving the summary (library name and basic code) about that.

Creating PDF File

Library: iTextSharp

Document <strong>document</strong> = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream
("C:\\test.pdf", System.IO.FileMode.Create));
document.Open();
document.Add(Paragraph p1 = new Paragraph("Hello World"););
document.Close();

Creating Word File

Library: Microsoft Word 14.0 Object Library (Internal)

Application app1 = new Application();
app1.Visible = true;
_Document doc1 = app1.Documents.Add();
doc1.Words.First.InsertBefore("Hello World");
doc1.SaveAs2("C:\\test.docx");

Creating Excel File

Library: Microsoft Excel 14.0 Object Library (Internal)

_Application app = new Application();
_Workbook workbook = app.Workbooks.Add(Type.Missing);
_Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Cells[1, 1] = "Hello World";
worksheet.Columns.AutoFit();
worksheet.Cells.HorizontalAlignment = Element.ALIGN_RIGHT;
workbook.SaveAs("C:\\test.xls", Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);

Creating Outlook Files (New Mail Window)

Library: Microsoft Outlook 14.0 Object Library (Internal)

Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)<strong>oApp</strong>.CreateItem
(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello World";
oMsg.Display();

Creating Text Files

Library: System.IO (Internal)

System.IO.File.WriteAllText("C:\test.txt", "Hello World");

Notes

To open a file, use this code:

System.Diagnostics.Process.Start("test.pdf");

Before using any library (dll), do not forget to add it as a reference (Visual Studio > Solution Explorer > Right Click on References > Add Reference):

Add Reference Windows in Visual Studio
Add Reference Window in Visual Studio

Solved: “Cygwin installation is out of date” error when you install Moshell on Cygwin

After install Cygwin by following this document (or this one as backup), the next step would be installing Moshell (Step 13 in that document). When you type bash moshell_install command, you see an error like this:

!!! Your cygwin installation is out of date and incompatible with the latest moshell version !!!
Please upgrade to the latest cygwin version by rerunning the setup.exe from www.cygwin.com/setup.exe

Cygwin Version Error
Cygwin Version Error

It is a common problem. You are not alone. To resolve this problem:

  • Check your cygwin version (Command: cygcheck -V)

    Cygwin Version Check
    Cygwin Version Check
  • Check www.cygwin.com for the latest version.
  • If you are sure that you have latest version, open “moshell_install” file (under “C:\cygwin\home\USERID“).

Change rows 201 between 209 as the screenshot below (add “#” symbols before commands):

The resolution
The resolution
Here you are:
Moshell Connection Successfull
Moshell Connection Successfull