C# Remote desktop application using RDP. How to generate the certificate ? - c#

I have some problems to use the MSTSCLib to connect from 1 PC to another one.
It's working with Servers but not with normal Workstations...
private void btn_connect_Click(object sender, EventArgs e)
{
try
{
rdp_control.Server = tbx_servername.Text;
rdp_control.Connect();
tabPage1.Text = "Connected";
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void btn_disconnect_Click(object sender, EventArgs e)
{
if (rdp_control.Connected.ToString() == "1")
{
rdp_control.Disconnect();
}
}
Both client and server applications are in the Same Network under the same NAT. Problem is the certificate,... I need to find a way to include the certificate. With the normal Remote Desktop from Windows, you see a MessageBox with the question:"Do you want to use this certificate.... blablabla" But this is not coming up with the RDP function in c#
Any Ideas?
Thanks B.R.

Following code shows a simple RDP client and server.
RDP Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDPCOMAPILib;
using AxMSTSCLib;
using System.Runtime.InteropServices;
namespace TCP_to_RDP_Converter
{
public partial class Form1 : Form
{
public static RDPSession currentSession = null;
public static void createSession()
{
currentSession = new RDPSession();
}
public static void Connect(RDPSession session)
{
session.OnAttendeeConnected += Incoming;
session.Open();
}
public static void Disconnect(RDPSession session)
{
session.Close();
}
public static string getConnectionString(RDPSession session, String authString,
string group, string password, int clientLimit)
{
IRDPSRAPIInvitation invitation =
session.Invitations.CreateInvitation
(authString, group, password, clientLimit);
return invitation.ConnectionString;
}
private static void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
/// <summary>
/// Handle the form items
/// </summary>
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
createSession();
Connect(currentSession);
textConnectionString.Text = getConnectionString(currentSession,
"Test","Group","",5);
}
private void button2_Click(object sender, EventArgs e)
{
Disconnect(currentSession);
}
}
}
In order to use the RDP communication library you need to add rdpcompapi and Microsoft windows terminal services, form the COM references.
RDP Client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDPCOMAPILib;
using AxRDPCOMAPILib;
namespace Simple_RDP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void Connect(string invitation, AxRDPViewer display, string userName, string password)
{
display.Connect(invitation, userName, password);
}
public static void disconnect(AxRDPViewer display)
{
display.Disconnect();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Connect(textConnectionString.Text, this.axRDPViewer, "", "");
}
catch (Exception)
{
MessageBox.Show("Unable to connect to the Server");
}
}
}
}
you can add reference to the AxRDPCOMAPILib by importing RDP viewer class component to the main form.
Full project can be downloaded from here [Download]:http://sandaruwmp.blogspot.com/2014/05/remote-desktop-application-with-rdp.html

use this....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MSTSCLib;
namespace RemoteTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Server = textBox1.Text;
MSTerminalServiceControl1.UserName = textBox2.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)MSTerminalServiceControl1.GetOcx();
secured.ClearTextPassword = textBox3.Text;
MSTerminalServiceControl1.Connect();
}
private void button2_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Disconnect();
}
}
}

Related

Error when using mySerialPort.PortName ()

I am developing a software in C #, and I need to list the names of the available serial ports, but when using (PortName ()) the Visual Studio does not recognize the (PortName ()) and the error. What could be wrong? I'm using Visual Studio 2019 and C #.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ListCOMs()
{
int i;
bool qtde;
i = 0;
qtde = false;
if (cbPorts.Items.Count == mySerialPort.PortName().Length)
{
foreach (string s in mySerialPort.PortName())
{
if (cbPorts.Items[i++].Equals(s) == false)
{
qtde = true;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timerCOM_Tick(object sender, EventArgs e)
{
ListCOMss();
}
}
}
Thanks

How to select sql database server address from drop down combobox at login page instead of hardcoding in c#?

I am using below code
public partial class Login : Form
{
string connectionString = #"server= SERVER ADDRESS;User id=UserName;Password=password;Initial Catalog=Databasename;Integrated Security=false";
Instead of coding SERVER ADDRESS in code, I want it to select from drop down as below
enter image description here
Try following :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProjectName
{
public partial class Login : Form
{
string[] servers = {"Server 1 Address", "Server 2 Address"};
string connectionString = "";
public Login()
{
InitializeComponent();
this.Load += new EventHandler(Login_Load);
}
private void Login_Load(object sender, EventArgs e)
{
listBox1.DataSource = servers;
listBox1.AllowDrop = true;
listBox1.SelectedValueChanged +=new EventHandler(listBox1_SelectedValueChanged);
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
connectionString = string.Format(#"server={0};User id=User;Password=Password;Initial Catalog=DATABASE;Integrated Security=false", listBox1.Text);
}
}
}

how to open other form after web fullyloaded c#

How to wait for web page fully loaded then open/create new form2 with webbrowser navigate to another page
I'm trying by checking using igetattribut on loaded site on form 1 but not working because checking is running before web fully loaded
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webbMain.Navigate("http://asia1.darkorbit.com/");
}
private void btnLogin_Click(object sender, EventArgs e)
{
var inputElements = webbMain.Document.GetElementsByTagName("input");
foreach (HtmlElement i in inputElements)
{
if (i.GetAttribute("name").Equals("username"))
{
i.InnerText = txtUsername.Text;
}
if (i.GetAttribute("name").Equals("password"))
{
i.Focus();
i.InnerText = txtPassword.Text;
}
}
var buttonElements = webbMain.Document.GetElementsByTagName("input");
foreach (HtmlElement b in inputElements)
{
if (b.GetAttribute("className").Equals("bgcdw_button bgcdw_login_form_login"))
{
b.InvokeMember("click");
}
}
}
}
}

Serial Port Data Received Event accessed from Multiple forms

I'm trying to make a windows form application in Visual Studio in C# and the purpose is to be able to communicate with an Arduino microcontroller. Right now I have 2 forms (Form1 and Form2) and I need to send and receive data from both windows to the microcontroller. I have defined a class SerialComms.cs in which there I start my serialPort and I have been able to send messages from both forms to the Arduino.
The problem is I don't know how to go about to receiving data from the Arduino as soon as the data is received. If I were to create the serialPort from one of the forms i would be able to create a function for DataReceived that will run anytime data is received but I don't know how to do that when I start my serialPort from a class. Any help?
Here is my code for both forms and my serialComms class:
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
connectToArduino();
}
void connectToArduino()
{
try { SerialComms.SerialPort.Close(); }
catch { }
getAvailablePorts();
for (int i = 0; i < COMcomboBox.Items.Count; i++)
{
string inputMsg = "None";
string commport = COMcomboBox.GetItemText(COMcomboBox.Items[i]);
//Wait for Arduino message sent
//If not receieved go to next port
SerialComms.SerialPort.PortName = commport;
SerialComms.SerialPort.BaudRate = 9600;
SerialComms.SerialPort.Open();
try { SerialComms.SerialPort.Open(); }
catch { }
int counter1 = 0;
while (counter1 < 100)
{
inputMsg = SerialComms.SerialPort.ReadLine();
try { inputMsg = SerialComms.SerialPort.ReadLine(); }
catch { }
counter1++;
if (inputMsg == "Arduino")
{
counter1 = 1000;
}
}
if (inputMsg != "Arduino")
{
try { SerialComms.SerialPort.Close(); }
catch { }
}
else
{
i = COMcomboBox.Items.Count;
SerialComms.SerialPort.WriteLine("Connection Established");
toolStripStatusLabel1.Text = "Connected";
break;
}
}
}
void getAvailablePorts()
{
COMcomboBox.Items.Clear();
String[] ports = SerialPort.GetPortNames();
COMcomboBox.Items.AddRange(ports);
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
}
private void button3_Click(object sender, EventArgs e) //Opens second form
{
Form2 settingsForm = new Form2();
settingsForm.ShowDialog();
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public partial class Form2 : Form
{
public static string text = "Test text";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
button2.Text = text;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
SerialComms.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public static class SerialComms
{
//public static delegate void DataReceivedEventHandler(object sender, ReceivedEventArgs e);
//public static event DataReceivedEventHandler DataReceived;
private static SerialPort _serialPort = new SerialPort();
public static SerialPort SerialPort
{
get { return _serialPort; }
set { _serialPort = value; }
}
}
}

How to transfer code from Console application to WinForms?

I want to transfer my code to a WinForms.
I know how to create buttons, text, etc... and how to operate them.
The problem is that I made my code in a windows application, and I want to transform it to WinForms application. I don't know how to copy and paste the whole code, cause in the WinForms application there is no main method..
Here is my code that I am trying to transfer to WinForms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClient connection = new TcpClient("127.0.0.1", 5000);
StreamReader sr = new StreamReader(connection.GetStream());
StreamWriter sw = new StreamWriter(connection.GetStream());
string name2 = "";
while (true)
{
Console.WriteLine("Enter your name and press submit");
name2=Console.ReadLine();
if (name2 != "")
{
sw.WriteLine(name2);
break;
}
}
Console.WriteLine("Loop is over");
Thread t2 = new Thread(Reader);
t2.IsBackground = true;
t2.Start(connection);
while (true)
{
sw.WriteLine(Console.ReadLine());
sw.Flush();
}
}
public static void Reader(object o)
{
TcpClient con = o as TcpClient;
if (con == null)
return;
StreamReader sr = new StreamReader(con.GetStream());
while (true)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine();
}
Console.WriteLine(sr.ReadLine());
Console.WriteLine();
}
}
}
}
Here is the Form that I have created:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
In windows form application there is main method but it is in program.cs file and its starts your default form after you program starts execution.
You can add the the textbox in form and on button event get the text of the textbox.
String username = textboxusername.Text;
Similarly you can do all the things you want.
The code that you have written before was for console application and that is not the same for the windows form application.
You have to code differently for this.

Categories