[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 求精通C#和PowerShell的大佬合并C#文件到PS中使用

说来真是折腾,
先是找的AHK的代码高亮组件, 对中文支持不好,
接着找的C++代码, 遇到各种语法问题,
今天在github搜的时候, 找到一个.net组件名字叫EasyScintilla 编译成了exe用了一下, 居然支持8种编程语言语法着色, 完全满足需要, 更重要的时候, 我看了下源代码,  实际代码只要不到七八十行,
重要代码分布在了如下的 Form1.cs和Form1.Designer.cs两个文件中, 怎样把这两个文件合并后, 嵌入到ps中来运行呢? 求路过高手支招, 谢谢
编译后的EXE和源代码在下面链接
https://t.wss.ink/f/awy92us406a 复制链接到浏览器打开
效果展示
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using EasyScintilla.Stylers;
  11. namespace ExampleProject
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private ExampleDisplay[] _examples = new ExampleDisplay[]
  16.         {
  17.             new ExampleDisplay("C#", "cs.txt", new CSharpStyler()),
  18.             new ExampleDisplay("C# (Dark)", "cs.txt", new DarkCSharpStyler()),
  19.             new ExampleDisplay("Windows Batch", "bat.txt", new BatchStyler()),
  20.             new ExampleDisplay("HTML", "html.txt", new HtmlStyler()),
  21.             new ExampleDisplay("JSON", "json.txt", new JsonStyler()),
  22.             new ExampleDisplay("PowerShell", "ps1.txt", new PowerShellStyler()),
  23.             new ExampleDisplay("Python", "py.txt", new PythonStyler()),
  24.             new ExampleDisplay("Ruby", "rb.txt", new RubyStyler()),
  25.             new ExampleDisplay("T-SQL", "sql.txt", new SqlStyler()),
  26.             new ExampleDisplay("Teradata Parallel Transporter", "tpt.txt", new TptStyler()),
  27.         };
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.         private void Form1_Load(object sender, EventArgs e)
  33.         {
  34.             stylerPicker.DataSource = _examples;
  35.             stylerPicker.DisplayMember = "Display";
  36.         }
  37.         private void stylerPicker_SelectedIndexChanged(object sender, EventArgs e)
  38.         {
  39.             var example = _examples[stylerPicker.SelectedIndex];
  40.             editor.Text = example.ReadFile();
  41.             editor.Styler = example.Styler;
  42.         }
  43.     }
  44. }
复制代码
  1. namespace ExampleProject
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// Required designer variable.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// Clean up any resources being used.
  11.         /// </summary>
  12.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows Form Designer generated code
  22.         /// <summary>
  23.         /// Required method for Designer support - do not modify
  24.         /// the contents of this method with the code editor.
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.editor = new EasyScintilla.SimpleEditor();
  29.             this.stylerPicker = new System.Windows.Forms.ComboBox();
  30.             this.SuspendLayout();
  31.             //
  32.             // editor
  33.             //
  34.             this.editor.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  35.             | System.Windows.Forms.AnchorStyles.Left)
  36.             | System.Windows.Forms.AnchorStyles.Right)));
  37.             this.editor.IndentationGuides = ScintillaNET.IndentView.LookBoth;
  38.             this.editor.Location = new System.Drawing.Point(12, 12);
  39.             this.editor.Name = "editor";
  40.             this.editor.Size = new System.Drawing.Size(854, 603);
  41.             this.editor.Styler = null;
  42.             this.editor.TabIndex = 0;
  43.             //
  44.             // stylerPicker
  45.             //
  46.             this.stylerPicker.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  47.             | System.Windows.Forms.AnchorStyles.Right)));
  48.             this.stylerPicker.FormattingEnabled = true;
  49.             this.stylerPicker.Location = new System.Drawing.Point(12, 621);
  50.             this.stylerPicker.Name = "stylerPicker";
  51.             this.stylerPicker.Size = new System.Drawing.Size(854, 21);
  52.             this.stylerPicker.TabIndex = 1;
  53.             this.stylerPicker.SelectedIndexChanged += new System.EventHandler(this.stylerPicker_SelectedIndexChanged);
  54.             //
  55.             // Form1
  56.             //
  57.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  58.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  59.             this.ClientSize = new System.Drawing.Size(878, 656);
  60.             this.Controls.Add(this.stylerPicker);
  61.             this.Controls.Add(this.editor);
  62.             this.Name = "Form1";
  63.             this.Text = "EasyScintilla Test App";
  64.             this.Load += new System.EventHandler(this.Form1_Load);
  65.             this.ResumeLayout(false);
  66.         }
  67.         #endregion
  68.         private EasyScintilla.SimpleEditor editor;
  69.         private System.Windows.Forms.ComboBox stylerPicker;
  70.     }
  71. }
复制代码

本帖最后由 小白龙 于 2023-4-7 22:07 编辑

我的想法是, 把上面的代码编辑器定义为一个函数, 关闭编辑器后, 返回修改后的代码! 基本代码如下:


需要引用的dll文件EasyScintilla.dll 在EXE文件夹中
  1. Add-Type -Path "$home\Desktop\EasyScintilla.dll"
  2. $batcode=@'
  3. @echo off
  4. set p=c:
  5. for /f "tokens=*" %%i in ('wmic logicaldisk where "name='%p%'" get DriveType^,Description^,name  /value') do (
  6.      for /f "tokens=*" %%j in ("%%i") do set %%j
  7. )
  8. echo %name%  %Description%
  9. pause
  10. '@
  11. $pscode=@'
  12. $st = Get-Date   #脚本开头
  13. Get-Process
  14. $et = Get-Date   #脚本结尾
  15. ($et - $st).TotalSeconds   #计算
  16. '@
  17. # Todo: 函数 EasySci代码
  18. # 使用函数EasySci
  19. easysci -code $batcode
  20. easysci -code $pscode
复制代码

TOP

没搞懂你要干什么(当然我没那能力帮你)

TOP

不懂winform,简单弄了下,机器太旧测试不了,不知道行不行,你自己试试看吧,出现找不到类型之类的自己尝试添加下winform或drawing或ScintillaNET.dll
  1. Add-Type -Path "$home\Desktop\EasyScintilla.dll"
  2. $batcode=@'
  3. @echo off
  4. set p=c:
  5. for /f "tokens=*" %%i in ('wmic logicaldisk where "name='%p%'" get DriveType^,Description^,name  /value') do (
  6.      for /f "tokens=*" %%j in ("%%i") do set %%j
  7. )
  8. echo %name%  %Description%
  9. pause
  10. '@
  11. $pscode=@'
  12. $st = Get-Date   #脚本开头
  13. Get-Process
  14. $et = Get-Date   #脚本结尾
  15. ($et - $st).TotalSeconds   #计算
  16. '@
  17. #不懂ps函数,弄成块了
  18. $easysci={
  19. param($thetext,$thetype)
  20. #编辑器设置
  21. $editor=[EasyScintilla.SimpleEditor]::new()
  22. $editor.Anchor = ([System.Windows.Forms.AnchorStyles](((([System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom) -bor [System.Windows.Forms.AnchorStyles]::Left) -bor [System.Windows.Forms.AnchorStyles]::Right)))
  23. $editor.IndentationGuides = [ScintillaNET.IndentView]::LookBoth
  24. $editor.Location = [System.Drawing.Point]::new(12, 12)
  25. $editor.Name = "editor"
  26. $editor.Size = [System.Drawing.Size]::new(854, 603)
  27. #$editor.Styler = $null
  28. if($thetype -eq "C#"){$editor.Styler=[EasyScintilla.Stylers.CSharpStyler]::new()}
  29. if($thetype -eq "C# (Dark)"){$editor.Styler=[EasyScintilla.Stylers.DarkCSharpStyler]::new()}
  30. if($thetype -eq "Windows Batch"){$editor.Styler=[EasyScintilla.Stylers.BatchStyler]::new()}
  31. if($thetype -eq "HTML"){$editor.Styler=[EasyScintilla.Stylers.HtmlStyler]::new()}
  32. if($thetype -eq "JSON"){$editor.Styler=[EasyScintilla.Stylers.JsonStyler]::new()}
  33. if($thetype -eq "PowerShell"){$editor.Styler=[EasyScintilla.Stylers.PowerShellStyler]::new()}
  34. if($thetype -eq "Python"){$editor.Styler=[EasyScintilla.Stylers.PythonStyler]::new()}
  35. if($thetype -eq "Ruby"){$editor.Styler=[EasyScintilla.Stylers.RubyStyler]::new()}
  36. if($thetype -eq "T-SQL"){$editor.Styler=[EasyScintilla.Stylers.SqlStyler]::new()}
  37. if($thetype -eq "Teradata Parallel Transporter"){$editor.Styler=[EasyScintilla.Stylers.TptStyler]::new()}
  38. $editor.Text=$thetext
  39. $editor.TabIndex = 0
  40. #窗体设置
  41. $theform=[System.Windows.Forms.Form]::new()
  42. $theform.SuspendLayout()
  43. $theform.AutoScaleDimensions=[System.Drawing.SizeF]::new(6, 13)
  44. $theform.AutoScaleMode=[System.Windows.Forms.AutoScaleMode]::font
  45. $theform.ClientSize=[System.Drawing.Size]::new(878, 656)
  46. $theform.Controls.Add($editor)
  47. $theform.Name = "Form1"
  48. $theform.Text = "EasyScintilla Test App"
  49. $theform.ResumeLayout($false)
  50. #显示窗体,模态
  51. $theform.ShowDialog()
  52. #清空
  53. $editor=$null
  54. $theform.Dispose()
  55. $theform=$null
  56. #返回
  57. return 0
  58. }
  59. #调用块,参数1为文本字符串,参数2为语言样式字符串
  60. $easysci.Invoke($batcode,'Windows Batch')
  61. #$easysci.Invoke($pscode,'PowerShell')
复制代码
1

评分人数

TOP

回复 4# Five66


    大佬, 高手, 成了!  太感谢了

TOP

本帖最后由 小白龙 于 2023-4-8 07:18 编辑

回复 3# jyswjjgdwtdtj

您也很热心, 帮了我不少忙, 同样感谢

TOP

回复 4# Five66

现在, 有个小问题, 关闭窗口后, 没有返回修改后的编辑器中的代码, 是不是要添加一个 确定 按钮, 才行呢? 怎么加呢

TOP

回复 7# 小白龙

在外面添加一个变量,清空前(第59行前)将$editor.Text赋值给这个变量,然后使用这个变量

TOP

本帖最后由 小白龙 于 2023-4-8 20:46 编辑

回复 8# Five66

大佬能再帮指导一下吗?
我用的软件虽然可以执行上面的PS脚本, 但是也是用命令行的方式调用的, 执行速度有点慢,
但是软件也能调用DLL, 以前在别的贴子中尝试过, 执行速度很快,
我大致知道了从C#代码转出COM版的DLL文件了, 但是不知道怎样把上面用到的两个CS文件合并为一个CS文件, 确切的说, 不知道怎样转成类库

估计应该要修改原C#代码, 把下拉列表组合框删除, 放上一个ok按钮
然后定义一个函数EasySci 返回值类型为string 参数有两个, 一个是要显示的代码, 另一个是代码类型
public string EasySci (string code, string codeType)

TOP

找了很多gpt文章, 没一个能用的, 想尝试问它, 也问不了,

TOP

本帖最后由 小白龙 于 2023-4-8 20:52 编辑

回复 4# Five66

大佬, 您改的PS代码,我能看懂, 但是源C#的代码,太啰嗦了, 好几个文件, 还一层套一层的, 搞不懂了

因为要生成类库, 所以不需要main函数, 就用我上面提到的函数名就行, 但是怎么改呢, Program.cs   Form1.cs    Form1.Designer.cs  这三个文件是不是都得要改一下

TOP

Program.cs   是执行程序代码
Form1.cs   界面交互代码
Form1.Designer.cs 界面设计代码
这三个文件基本上是可以并一起的,只要调整下那些using

至于弄成你说的类库,参考下面的,C#跟winform都只是略懂毛毛雨,不知对不对
EasySci函数(第123行)定义在Nakano类(第105行)中
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using EasyScintilla.Stylers;
  11. using System.IO;
  12. using System.Reflection;
  13. namespace ExampleProject
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private EasyScintilla.SimpleEditor editor;//编辑器声明
  18.         private System.Windows.Forms.Button theButton;//按钮声明
  19.         private String thertStr //返回字符串声明
  20.          ///<summary>
  21.         /// Required designer variable.
  22.         /// </summary>
  23.         private System.ComponentModel.IContainer components = null;
  24.         /// <summary>
  25.         /// Clean up any resources being used.
  26.         ///重定义Form1释放函数
  27.         /// </summary>
  28.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  29.         protected override void Dispose(bool disposing)
  30.         {
  31.             if (disposing && (components != null))
  32.             {
  33.                 components.Dispose();
  34.             }
  35.             base.Dispose(disposing);
  36.         }
  37.         //定义Form1关闭事件函数,用于关闭时储存编辑器文本
  38.         private void Form1_Closing(object sender, EventArgs e){thertStr=editor.Text;}
  39.         //Form1构造函数
  40.         public Form1()
  41.         {
  42.             InitializeComponent();//调用初始化函数
  43.         }
  44.         #region Windows Form Designer generated code
  45.         /// <summary>
  46.         /// Required method for Designer support - do not modify
  47.         /// the contents of this method with the code editor.
  48.         ///初始化函数定义
  49.         /// </summary>
  50.         private void InitializeComponent()
  51.         {
  52.             this.editor = new EasyScintilla.SimpleEditor();//创建编辑器
  53.             this.theButton = new System.Windows.Forms.Button();//创建按钮
  54.             this.SuspendLayout();
  55.             //
  56.             // editor设置
  57.             //
  58.             this.editor.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  59.             | System.Windows.Forms.AnchorStyles.Left)
  60.             | System.Windows.Forms.AnchorStyles.Right)));
  61.             this.editor.IndentationGuides = ScintillaNET.IndentView.LookBoth;
  62.             this.editor.Location = new System.Drawing.Point(12, 12);
  63.             this.editor.Name = "editor";
  64.             this.editor.Size = new System.Drawing.Size(854, 603);
  65.             this.editor.Styler = null;
  66.             this.editor.TabIndex = 0;
  67.             //
  68.             // 按钮设置
  69.             //
  70.             this.theButton.Size=new System.Drawing.Size(100, 21);
  71.             this.theButton.Location=new System.Drawing.Point(12, 621);
  72.             this.theButton.Name="button1";
  73.             this.theButton.Text="OK";
  74.             //
  75.             // Form1设置
  76.             //
  77.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  78.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  79.             this.ClientSize = new System.Drawing.Size(878, 656);
  80.             this.Controls.Add(this.theButton);//添加按钮到Form1
  81.             this.Controls.Add(this.editor);//添加编辑器到Form1
  82.             this.Name = "Form1";
  83.             this.Text = "EasyScintilla Test App";
  84.             //this.Load += new System.EventHandler(this.Form1_Load);
  85.             this.Closing += new System.EventHandler(this.Form1_Closing);//添加关闭事件
  86.             this.ResumeLayout(false);
  87.         }
  88.         #endregion
  89.     }
  90. //========================================
  91. //========================================
  92. public class Nakano{
  93. //语言样式字段
  94. private Hashtable _theStyler=new Hashtable();
  95. _theStyler.Add("C#", new CSharpStyler());
  96. _theStyler.Add("C# (Dark)", new DarkCSharpStyler());
  97. _theStyler.Add("Windows Batch" new BatchStyler());
  98. _theStyler.Add("HTML",  new HtmlStyler());
  99. _theStyler.Add("JSON",  new JsonStyler());
  100. _theStyler.Add("PowerShell",  new PowerShellStyler());
  101. _theStyler.Add("Python", new PythonStyler());
  102. _theStyler.Add("Ruby", new RubyStyler());
  103. _theStyler.Add("T-SQL", new SqlStyler());
  104. _theStyler.Add("Teradata Parallel Transporter", new TptStyler());
  105. //语言样式属性
  106. public static Hashtable TheStyler{get {return _theStyler;}}
  107. public static string EasySci (string code, string codeType)
  108. {
  109. var theForm1=new Form1();
  110. string theStr='';
  111. if(_theStyler.ContainsKey(codeType))
  112. {
  113. theForm1.editor.Styler=_theStyler[codeType];
  114. }
  115. theForm1.editor.Text=code;
  116. theForm1.ShowDialog();
  117. theStr=new String(theForm1.thertStr);
  118. theForm1.Dispose();
  119. return theStr;
  120. }
  121. }
  122. //========================================
  123. //========================================
  124. }
复制代码

TOP

回复 12# Five66

多谢大佬,

109-118行, 都报下面的错误

error CS1519: 类、结构或接口成员声明中的标记“(”无效
error CS1520: 方法必须具有返回类型
error CS1002: 应输入 ;

TOP

本帖最后由 小白龙 于 2023-4-9 08:50 编辑

回复 12# Five66


    问了一下gpt

这段代码中有以下几个问题:

在定义语言样式字段 _theStyler 时,应该在方法或构造函数中进行初始化,而不是在类体外进行初始化。
在定义语言样式字段 _theStyler 时,缺少逗号。
在 EasySci 方法中使用 _theStyler 时,应该使用 Nakano._theStyler,因为 _theStyler 是 Nakano 类的字段。
在调用 Dispose() 方法之前,应该先隐藏窗口并将其设置为不可见。
下面是修改后的代码:
  1. using System;
  2. using System.Collections;
  3. using System.Windows.Forms;
  4. using EasyScintilla.Stylers;
  5. namespace ExampleProject
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         private readonly EasyScintilla.SimpleEditor editor;//编辑器声明
  10.         private readonly Button theButton;//按钮声明
  11.         public string ThertStr; //返回字符串声明
  12.         /// <summary>
  13.         /// Required designer variable.
  14.         /// </summary>
  15.         private readonly System.ComponentModel.IContainer components = null;
  16.         /// <summary>
  17.         /// Clean up any resources being used.
  18.         ///重定义Form1释放函数
  19.         /// </summary>
  20.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  21.         protected override void Dispose(bool disposing)
  22.         {
  23.             if (disposing && (components != null))
  24.             {
  25.                 components.Dispose();
  26.             }
  27.             base.Dispose(disposing);
  28.         }
  29.         //定义Form1关闭事件函数,用于关闭时储存编辑器文本
  30.         private void Form1_Closing(object sender, EventArgs e)
  31.         {
  32.             ThertStr = editor.Text;
  33.         }
  34.         //Form1构造函数
  35.         public Form1()
  36.         {
  37.             InitializeComponent();//调用初始化函数
  38.             editor = new EasyScintilla.SimpleEditor();//创建编辑器
  39.             theButton = new Button();//创建按钮
  40.             this.SuspendLayout();
  41.             //
  42.             // editor设置
  43.             //
  44.             editor.Anchor = ((AnchorStyles) (((AnchorStyles.Top | AnchorStyles.Bottom)
  45.                                                 | AnchorStyles.Left)
  46.                                                | AnchorStyles.Right));
  47.             editor.IndentationGuides = ScintillaNET.IndentView.LookBoth;
  48.             editor.Location = new System.Drawing.Point(12, 12);
  49.             editor.Name = "editor";
  50.             editor.Size = new System.Drawing.Size(854, 603);
  51.             editor.Styler = null;
  52.             editor.TabIndex = 0;
  53.             //
  54.             // 按钮设置
  55.             //
  56.             theButton.Size=new System.Drawing.Size(100, 21);
  57.             theButton.Location=new System.Drawing.Point(12, 621);
  58.             theButton.Name="button1";
  59.             theButton.Text="OK";
  60.             this.Controls.Add(theButton);//添加按钮到Form1
  61.             this.Controls.Add(editor);//添加编辑器到Form1
  62.             this.ResumeLayout(false);
  63.         }
  64.     }
  65.     //========================================
  66.     public class Nakano {
  67.         //语言样式字段
  68.         private static readonly Hashtable _theStyler=new Hashtable() {
  69.             {"C#", new CSharpStyler()},
  70.             {"C# (Dark)", new DarkCSharpStyler()},
  71.             {"Windows Batch", new BatchStyler()},
  72.             {"HTML", new HtmlStyler()},
  73.             {"JSON", new JsonStyler()},
  74.             {"PowerShell", new PowerShellStyler()},
  75.             {"Python", new PythonStyler()},
  76.             {"Ruby", new RubyStyler()},
  77.             {"T-SQL", new SqlStyler()},
  78.             {"Teradata Parallel Transporter", new TptStyler()}
  79.         };
  80.         //语言样式属性
  81.         public static Hashtable TheStyler => _theStyler;
  82.         
  83.         public static string EasySci(string code, string codeType)
  84.         {
  85.             var theForm1 = new Form1();
  86.             var theStr = "";
  87.             if (_theStyler.ContainsKey(codeType))
  88.             {
  89.                 theForm1.editor.Styler = (IStyler) _theStyler[codeType];
  90.             }
  91.         
  92.             theForm1.editor.Text = code;
  93.         
  94.             theForm1.ShowDialog();
  95.         
  96.             theStr = theForm1.ThertStr;
  97.         
  98.             theForm1.Hide();
  99.             theForm1.Visible = false;
  100.             theForm1.Dispose();
  101.         
  102.             return theStr;
  103.         }
  104.     }
  105. //========================================
  106. }
复制代码

TOP

回复 12# Five66


    GPT答案, 下面这行,还是提示有错误
        //语言样式属性
        public static Hashtable TheStyler => _theStyler;

--------------------------------------------------------------------
error CS1002: 应输入 ;
error CS1519: 类、结构或接口成员声明中的标记“;”无效.

TOP

返回列表