鱼C论坛

 找回密码
 立即注册
查看: 873|回复: 5

学生因大作业需求,向大佬们请教下c#连接rdp和remoteapp的方法,可以有偿

[复制链接]
发表于 2023-6-18 09:28:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学生因大作业需求,向大佬们请教下c#连接rdp和remoteapp的方法,可以有偿
比如这个怎么利用c#程序实现链接这个rdp文件
allow desktop composition:i:1
allow font smoothing:i:1
alternate full address:s:119.91.206.185
alternate shell:s:rdpinit.exe
devicestoredirect:s:*
disableremoteappcapscheck:i:1
drivestoredirect:s:*
full address:s:119.91.206.185
prompt for credentials on client:i:1
promptcredentialonce:i:0
redirectcomports:i:1
redirectdrives:i:1
remoteapplicationmode:i:1
remoteapplicationname:s:AliWorkbench
remoteapplicationprogram:s:||AliWorkbench
span monitors:i:1
use multimon:i:1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-6-18 09:30:50 | 显示全部楼层
q3455214945
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-18 10:22:12 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-6-18 10:26 编辑

您好,连接 RDP 和 RemoteApp 的方法在 C# 中可以使用 System.Net.Sockets 命名空间中的 TcpClient 类实现。可以参考以下代码实现:

  1. using System;
  2. using System.Net.Sockets;

  3. namespace ConnectRdpDemo
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string rdpFileContent = "allow desktop composition:i:1\n" +
  10.                                     "allow font smoothing:i:1\n" +
  11.                                     "alternate full address:s:119.91.206.185\n" +
  12.                                     "alternate shell:s:rdpinit.exe\n" +
  13.                                     "devicestoredirect:s:*\n" +
  14.                                     "disableremoteappcapscheck:i:1\n" +
  15.                                     "drivestoredirect:s:*\n" +
  16.                                     "full address:s:119.91.206.185\n" +
  17.                                     "prompt for credentials on client:i:1\n" +
  18.                                     "promptcredentialonce:i:0\n" +
  19.                                     "redirectcomports:i:1\n" +
  20.                                     "redirectdrives:i:1\n" +
  21.                                     "remoteapplicationmode:i:1\n" +
  22.                                     "remoteapplicationname:s:AliWorkbench\n" +
  23.                                     "remoteapplicationprogram:s:||AliWorkbench\n" +
  24.                                     "span monitors:i:1\n" +
  25.                                     "use multimon:i:1";

  26.             string[] rdpParameters = rdpFileContent.Split('\n');

  27.             TcpClient tcpClient = new TcpClient();
  28.             tcpClient.Connect("119.91.206.185", 3389);

  29.             NetworkStream networkStream = tcpClient.GetStream();

  30.             foreach (var parameter in rdpParameters)
  31.             {
  32.                 byte[] buffer = System.Text.Encoding.ASCII.GetBytes(parameter + "\r\n");
  33.                 networkStream.Write(buffer, 0, buffer.Length);
  34.             }

  35.             networkStream.Close();
  36.             tcpClient.Close();
  37.         }
  38.     }
  39. }
复制代码

其中,TcpClient.Connect 方法用于连接 RDP,NetworkStream.Write 方法用于发送 RDP 参数。您需要根据您的 RDP 文件进行相应修改,并将代码嵌入到您的 C# 程序中即可实现连接 RDP 和 RemoteApp。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-18 11:36:29 | 显示全部楼层
您好!要使用C#程序连接RDP及RemoteApp需要使用`MSTSCLib`库。

  1. using System;
  2. using System.Windows.Forms;
  3. using MSTSCLib;

  4. namespace RDP_Connection
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }

  12.         private void bConnect_Click(object sender, EventArgs e)
  13.         {
  14.             try
  15.             {
  16.                 // 配置RDP控件参数
  17.                 axRDPControl.DesktopHeight = 1024;
  18.                 axRDPControl.DesktopWidth = 1280;
  19.                 axRDPControl.AdvancedSettings7.SmartSizing = true;
  20.                 axRDPControl.ColorDepth = 32;

  21.                 // 设置RDP文件信息
  22.                 IMsTscNonScriptable secured = (IMsTscNonScriptable)axRDPControl.GetOcx();
  23.                 secured.ClearTextPassword = "mypassword"; // RDP密码
  24.                 axRDPControl.Server = "myserver.com"; // RDP server地址
  25.                 axRDPControl.UserName = "myusername"; // RDP登录用户名
  26.                 axRDPControl.Domain = null; // RDP登录域

  27.                 // 使用上面的rdp配置信息链接RDP
  28.                 axRDPControl.Connect();
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 MessageBox.Show(ex.Message);
  33.             }
  34.         }

  35.         private void bDisconnect_Click(object sender, EventArgs e)
  36.         {
  37.             axRDPControl.Disconnect(); // 断开RDP连接
  38.         }
  39.     }
  40. }
复制代码


对于RemoteApp连接,建议将其部署在Web服务器中并运行。以下是一个示例:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     MsRdpClient8NotSafeForScripting client = new MsRdpClient8NotSafeForScripting();
  4.     client.Server = "myserver.com";
  5.     client.AdvancedSettings7.EnableCredSspSupport = true;
  6.     client.AdvancedSettings7.AuthenticationLevel = 2;
  7.     client.UserName = "myusername";
  8.     client.SecuredSettings.FullScreen = false;

  9.     //RemoteApp连接
  10.     IMsTscNonScriptable secured = (IMsTscNonScriptable)client.GetOcx();
  11.     secured.ClearTextPassword = "mypassword"; // RemoteApp登录密码
  12.     client.AdvancedSettings8.RDPPort = 3389; // 连接端口,默认为3389
  13.     client.AdvancedSettings8.RemoteProgram2 = "rdpinit.exe"; // RemoteApp程序名
  14.     client.AdvancedSettings8.RemoteApplicationMode = true; // 启用RemoteApp模式
  15.     client.Connect();
  16. }
复制代码

希望这些代码能够帮助您连接RDP和RemoteApp。谢谢!

求最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-21 22:25:33 | 显示全部楼层
问题解决了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-22 16:49:07 | 显示全部楼层
您好!连接 RDP 和 RemoteApp 可以使用 C# 编程语言来实现。以下是一个示例代码,该代码会读取给定的 RDP 文件,并使用指定的参数进行远程连接:

  1. using System;
  2. using System.Diagnostics;

  3. namespace RdpConnectionExample
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string rdpFileContent = @"
  10. allow desktop composition:i:1
  11. allow font smoothing:i:1
  12. alternate full address:s:119.91.206.185
  13. alternate shell:s:rdpinit.exe
  14. devicestoredirect:s:*
  15. disableremoteappcapscheck:i:1
  16. drivestoredirect:s:*
  17. full address:s:119.91.206.185
  18. prompt for credentials on client:i:1
  19. promptcredentialonce:i:0
  20. redirectcomports:i:1
  21. redirectdrives:i:1
  22. remoteapplicationmode:i:1
  23. remoteapplicationname:s:AliWorkbench
  24. remoteapplicationprogram:s:||AliWorkbench
  25. span monitors:i:1
  26. use multimon:i:1";

  27.             string rdpFilePath = @"C:\path\to\example.rdp";

  28.             // 将 RDP 文件内容写入指定文件
  29.             System.IO.File.WriteAllText(rdpFilePath, rdpFileContent);

  30.             // 使用默认关联程序打开 RDP 文件
  31.             Process.Start(rdpFilePath);
  32.         }
  33.     }
  34. }
复制代码

请将上述示例代码中的 rpFileContent 替换为您的 RDP 文件内容,并且将 rdpFilePath 替换为希望保存 RDP 文件的路径。运行代码后,它将创建一个 RDP 文件并将其打开。

这只是一个基本的示例,具体的实现取决于您的需求和环境。您可以根据需要进一步定制和扩展代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 17:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表