c# - 通过命令提示符运行表单应用程序并向其发送命令
问题描述
我在 VB 中有一个 Windows 窗体工具,我已经研究了一段时间。现在我希望能够通过命令提示符访问所有控件并返回值,以便我能够通过 Azure 使用它并基本上使应用程序成为一个黑匣子。
所以这就是我开始思考我应该这样做的方式。
1 - 在我的项目中,我创建了第二个解决方案,即 C# Windows 命令行框架。
2 - 我将以下脚本添加到第二个项目中以运行 win 表单
using EnabledTest;
using System;
using System.Windows.Forms;
namespace Command_lineStartup
{
internal class Program
{
private static frmMain MainForm;
[STAThread]
private static void Main(string[] args)
{
if (args.Length > 0)
{
// Command line given, display console
}
else
{
AllocConsole();
ConsoleMain(args);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(MainForm = new frmMain());
GUI();
}
}
private static void ConsoleMain(string[] args)
{
Console.WriteLine("Command line = {0}", Environment.CommandLine);
for (int ix = 0; ix < args.Length; ++ix)
Console.WriteLine("Argument{0} = {1}", ix + 1, args[ix]);
// Console.ReadLine();
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();
public static void GUI()
{
Console.WriteLine("Testing version 1 :");
Console.WriteLine("Enter Project File path to open, Project must be a .mmp file");
string Path = Console.ReadLine();
MainForm.LoadProject(Path);
}
}
}
但是,我认为这不是正确的方法。当我使用 C:\TFS\Enabled Test\Command-lineStartup\bin\Debug\Command-lineStartup.exe 通过 CMD 运行控制台应用程序时”
我没有表现出我希望它工作的方式。
所以我的问题是。我会以正确的方式解决这个问题吗?如果是这样,我在这里做错了什么
有没有更简单的方法?
解决方案
所以我最终找到了一个方法。通过 CMD 运行应用程序时,提供的参数可以使用
Dim cla As String() = Environment.GetCommandLineArgs()
然后对于提供的每个参数,你可以用它做一些事情。例如,
If cla.Length > 1 Then
'cla(0) is the executable path.
'cla(1) is the Path to the project
If Not IsIDE() Then WCLicenseIsLicensed("Application", True)
Me.Text = Application.ProductName
mblnLoaded = True
LoadProject(cla(1))
TreeVieuwSystem.Nodes(cla(2)).Expand()
TreeVieuwSystem.SelectedNode = TreeVieuwSystem.Nodes(cla(2)).Nodes.Find(cla(2) & "\" & cla(3), True).First
NodeSelected()
If cla(2) = "Test Plans" Then
TheWindowThatAllowsYouToEditTheObject.RunTestPlan()
ElseIf cla(2) = "Tests" Then
TheWindowThatAllowsYouToEditTheObject.RunTest(False)
End If
Else
推荐阅读
- database - 如何正确实现存储库模式?
- raspberry-pi4 - archlinux arm的pygtk包
- shell - shell 无法识别 graddle 包装器
- google-analytics-api - Google Analytics Core 报告 API v3 需要 4 分钟响应
- android - FLAG_KEEP_SCREEN_ON 的对面
- go - 读取和写入之间aws DynamoDB上的事务处理
- sql - 遍历 JSON 数组中的每个元素并加入行
- c++ - 我需要一些关于动态规划的建议
- laravel - php工匠存储
- python - 我的代码只生成了 2 个管道,但我想要生成无限数量的管道