C# 字符串复制到剪贴板错误

C# 字符串复制到剪贴板错误,c#,runtime,clipboard,C#,Runtime,Clipboard,我正在尝试读取一个文件,其中包含一些我希望复制到剪贴板的命令。在互联网搜索中,我找到了一种方法,可以将数据复制到剪贴板,我已经成功地做到了。但是,我必须复制多个命令。我正在做一个while循环。这是我的密码 { class Program { [DllImport("user32.dll")] internal static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllI

我正在尝试读取一个文件,其中包含一些我希望复制到剪贴板的命令。在互联网搜索中,我找到了一种方法,可以将数据复制到剪贴板,我已经成功地做到了。但是,我必须复制多个命令。我正在做一个while循环。这是我的密码

{
    class Program
    {
        [DllImport("user32.dll")]
        internal static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("user32.dll")]
        internal static extern bool CloseClipboard();

        [DllImport("user32.dll")]
        internal static extern bool SetClipboardData(uint uFormat, IntPtr data);

        [STAThread]
        static void Main(string[] args)
        {
            int counter = 0;
            string line;
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\st4r8_000\Desktop\office work\checks documents\interface check commands.txt");
            OpenClipboard(IntPtr.Zero);
            //int x;
            while((line = file.ReadLine()) != null)
            {
                Console.WriteLine (line);

                //clip board copier

                var yourString = line;
                var ptr = Marshal.StringToHGlobalUni(yourString);
                SetClipboardData(13, ptr);

                Marshal.FreeHGlobal(ptr);
                Console.ReadLine();
                //end of clip board copier
                counter++;
                //ptr = x;

            }
            CloseClipboard();
            file.Close();
            // Suspend the screen.
            Console.ReadLine();
        }
    }
}
因此,我发现的问题在下面的一行
Marshal.FreeHGlobal(ptr)
或者可能在
SetClipboardData(13,ptr)中但我不知道如何解决这个问题。这在第一次运行时运行得很好,但在第二次或第三次运行时,程序停止响应。任何帮助都将不胜感激


我没有使用windows窗体。我正在尝试在控制台中构建它。

似乎您不想要所有pInvoke的东西,而是
剪贴板。SetText

using System.Windows.Forms; // to have "Clipboard" class
using System.IO;            // to have "File" class

   ...
班级计划 {


首先,为什么要使用user32.dll?有一个剪贴板类和SetText方法(在中作了详细解释)为什么不使用
Clipboard.SetText()
?@dioloatom,因为我不使用windows窗体。所以我找到了这个解决方案。@DmitryBychenko有
Clipboard.SetText()
在控制台应用程序中工作?@Aqeel Abbas:Clipboard.SetText()也应该与控制台应用程序一起使用;是的,
剪贴板
系统.Windows.Forms
中,这是误导性的。我对c#非常陌生。我在
foreach
行中遇到了问题。它说不能将类型char转换为字符串。请检查@Aqeel Abbas:确保您有两个使用(请参阅我的编辑),根本不要使用
StreamReader
-请参阅我的代码:
File.ReadLines(…)
;最后一个错误是我的错误(键入)
if(clipBuffer.Length>0)
我正在创建控制台应用程序。这就是我使用dll的原因。我不认为在控制台应用程序中可以使用
System.Windows.Forms
。@Aqeel Abbas:为什么不可以?你可以使用你想要的任何程序集。我明白了。我很抱歉,因为我不在其中。我在那里添加了一个引用,现在可以了:)
    [STAThread]
    static void Main(string[] args)
    {
        var lines = File.ReadLines(@"C:\Users\st4r8_000\Desktop\office work\checks documents\error log check.txt");

        StringBuilder clipBuffer = new StringBuilder();

        foreach (String line in lines)
        {
            Console.WriteLine(line);

            if (clipBuffer.Length > 0)
                clipBuffer.Append('\n');

            clipBuffer.Append(line);
            Clipboard.SetText(line);
            // Incremental addition; 
            // Clipboard.SetText(line); 
            // if new line should superecede the old one
            //Clipboard.SetText(clipBuffer.ToString());

            Console.ReadLine();
        }
        // Suspend the screen.
        Console.ReadLine();
    }
}