[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]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

回复 4# Five66


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

TOP

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

回复 3# jyswjjgdwtdtj

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

TOP

回复 4# Five66

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

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

回复 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

回复 16# Five66


    theForm1.editor.Styler=_theStyler[codeType];
会报错
error CS0266: 无法将类型“object”隐式转换为“EasyScintilla.Stylers.ScintillaStyler”。存在一个显式转换(是否缺少强制转换?).
我改成
theForm1.editor.Styler=(ScintillaStyler)_theStyler[codeType];
不报错了, 但是又报下面的错了, 两个dll都引用了EasyScintilla.dll   ScintillaNET.dll

        Could not load the Scintilla module at the path 'C:\Users\Administrator\AppData\Local\Temp\ScintillaNET\3.6.3\x86\SciLexer.dll'.
        在 ScintillaNET.Scintilla.get_CreateParams()
        在 System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
        在 ScintillaNET.Scintilla..ctor()
        在 EasyScintilla.SimpleEditor..ctor()
        在 ExampleProject.Form1.InitializeComponent()
        在 ExampleProject.Form1..ctor()
        在 ExampleProject.Nakano.EasySci(String code, String codeType).

TOP

回复 16# Five66


   是不是要把 EasyScintilla文件夹下的SimpleEditor.cs文件也合并进来? 这个文件太大了10K
感觉是不是哪里有问题, 用powershell几行代码就搞定了, 但用C#有点复杂, 是不是不合并也行呢?

powershell代码只需要那两个dll就行了

TOP

private EasyScintilla.SimpleEditor editor;//编辑器声明
                private System.Windows.Forms.Button theButton;//按钮声明
                private String thertStr; //返回字符串声明

我也改成了下面这样, 不然会提示   不可访问,因为它受保护级别限制

                public EasyScintilla.SimpleEditor editor;//编辑器声明
                public System.Windows.Forms.Button theButton;//按钮声明
                public String thertStr; //返回字符串声明

TOP

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

回复 20# Five66

C:\Users\Administrator\AppData\Local\Temp\ScintillaNET\3.6.3\x86\SciLexer.dll
    我在报错的这个路径下看了一下, 是有这个dll文件的, 这个文件, 应该是打包在ScintillaNET.dll中了, 执行的时候就解包了?

TOP

返回列表