问题
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.
回答1:
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
回答2:
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();
}
}
}
来源:https://stackoverflow.com/questions/23545717/c-sharp-remote-desktop-application-using-rdp-how-to-generate-the-certificate