将文本文件读入剪贴板

本文关键字:剪贴板 文件 文本 | 更新日期: 2023-09-27 18:29:29

经过一段时间的谷歌搜索,我决定在这里发布我的问题
第一:我完全是C
我正在使用Jitbit中的宏录制器,我别无选择,只能使用其他录制器。问题是在宏记录器中,它缺少一些重要的东西
类似于将文本文件读取到变量中并通过剪贴板粘贴此变量:-(然而,好的一面是,该工具支持"某些"类型的原生C#代码

如果我打开C#命令,我会得到这个:

public class Program
{
    public static void Main()
    {
        System.Windows.Forms.MessageBox.Show("test");
    }
}

C#程序还必须遵循以下规则:=>此代码必须包含一个名为"Program"、静态方法为"Main"的类
我已经使用了谷歌,找到了应该做这项工作的代码,但我遇到了错误,我想代码不遵循上述规则
这就是我发现并尝试的:

using System;
using System.IO;
public class Program
{
    public static void Main()
    {
    // Read the file as one string.
    System.IO.StreamReader myFile =
     new System.IO.StreamReader("Counter.txt");
    string counter = myFile.ReadToEnd();
    myFile.Close();
    // Load string into clipboard
    Clipboard.SetDataObject( counter, true );
    }
}

我总是收到错误:"第15行:上下文中不存在名称剪贴板"?!?我希望有人能向我解释什么是错误的,什么是正确的代码。谢谢

将文本文件读入剪贴板

添加对System.Windows.Forms 的引用

using System;
using System.IO;
using System.Windows.Forms;
public class Program
{
    [STAThread]
    public static void Main()
    {
        Clipboard.SetDataObject(File.ReadAllText("Counter.txt"), true);
    }
}

请注意,要避免ThreadStateException,您需要将STAThread属性应用于Main()函数