.Net Core - 复制到剪贴板?

2024-02-05

Is it possible to copy something to the clipboard using .Net Core (in a platform-agnostic way)?

看来Clipboard https://msdn.microsoft.com/en-us/library/system.windows.clipboard(v=vs.110).aspx缺少类,并且 P/Invoking 不是 Windows 之外的选项。

Edit: Unfortunately until now there's been a discrepancy between what my question said and what people heard when they read the question. Based on the comments and answers two things are clear. First, very few care whether the truest kind of "ivory tower" platform agnosticism exists or not. Second, when people post code examples showing how you use the clipboard on different platforms then the technically correct answer ("no, it's not possible https://stackoverflow.com/a/49459767/3063273") is confusing. So I have struck the parenthetical clause.


我的这个项目(https://github.com/SimonCropp/TextCopy https://github.com/SimonCropp/TextCopy)使用 PInvoke 和命令行调用的混合方法。目前它支持

  • 装有 .NET Framework 4.6.1 及更高版本的 Windows
  • 带有 .NET Core 2.0 及更高版本的 Windows
  • 装有 Mono 5.0 及更高版本的 Windows
  • 带有 .NET Core 2.0 及更高版本的 OSX
  • OSX 与 Mono 5.20.1 及更高版本
  • 带有 .NET Core 2.0 及更高版本的 Linux
  • 采用 Mono 5.20.1 及更高版本的 Linux

Usage:

Install-Package TextCopy

TextCopy.ClipboardService.SetText("Text to place in clipboard");

或者直接使用实际的代码

Windows

https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/WindowsClipboard.cs https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/WindowsClipboard.cs

static class WindowsClipboard
{
    public static void SetText(string text)
    {
        OpenClipboard();

        EmptyClipboard();
        IntPtr hGlobal = default;
        try
        {
            var bytes = (text.Length + 1) * 2;
            hGlobal = Marshal.AllocHGlobal(bytes);

            if (hGlobal == default)
            {
                ThrowWin32();
            }

            var target = GlobalLock(hGlobal);

            if (target == default)
            {
                ThrowWin32();
            }

            try
            {
                Marshal.Copy(text.ToCharArray(), 0, target, text.Length);
            }
            finally
            {
                GlobalUnlock(target);
            }

            if (SetClipboardData(cfUnicodeText, hGlobal) == default)
            {
                ThrowWin32();
            }

            hGlobal = default;
        }
        finally
        {
            if (hGlobal != default)
            {
                Marshal.FreeHGlobal(hGlobal);
            }

            CloseClipboard();
        }
    }

    public static void OpenClipboard()
    {
        var num = 10;
        while (true)
        {
            if (OpenClipboard(default))
            {
                break;
            }

            if (--num == 0)
            {
                ThrowWin32();
            }

            Thread.Sleep(100);
        }
    }

    const uint cfUnicodeText = 13;

    static void ThrowWin32()
    {
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool CloseClipboard();

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);

    [DllImport("user32.dll")]
    static extern bool EmptyClipboard();
}

macOS

https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/OsxClipboard.cs https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/OsxClipboard.cs

static class OsxClipboard
{
    public static void SetText(string text)
    {
        var nsString = objc_getClass("NSString");
        IntPtr str = default;
        IntPtr dataType = default;
        try
        {
            str = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), text);
            dataType = objc_msgSend(objc_msgSend(nsString, sel_registerName("alloc")), sel_registerName("initWithUTF8String:"), NSPasteboardTypeString);

            var nsPasteboard = objc_getClass("NSPasteboard");
            var generalPasteboard = objc_msgSend(nsPasteboard, sel_registerName("generalPasteboard"));

            objc_msgSend(generalPasteboard, sel_registerName("clearContents"));
            objc_msgSend(generalPasteboard, sel_registerName("setString:forType:"), str, dataType);
        }
        finally
        {
            if (str != default)
            {
                objc_msgSend(str, sel_registerName("release"));
            }

            if (dataType != default)
            {
                objc_msgSend(dataType, sel_registerName("release"));
            }
        }
    }

    [DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
    static extern IntPtr objc_getClass(string className);

    [DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
    static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector);

    [DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
    static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector, string arg1);

    [DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
    static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);

    [DllImport("/System/Library/Frameworks/AppKit.framework/AppKit")]
    static extern IntPtr sel_registerName(string selectorName);

    const string NSPasteboardTypeString = "public.utf8-plain-text";
}

Linux

https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/LinuxClipboard_2.1.cs https://github.com/CopyText/TextCopy/blob/master/src/TextCopy/LinuxClipboard_2.1.cs

static class LinuxClipboard
{
    public static void SetText(string text)
    {
        var tempFileName = Path.GetTempFileName();
        File.WriteAllText(tempFileName, text);
        try
        {
            BashRunner.Run($"cat {tempFileName} | xclip");
        }
        finally
        {
            File.Delete(tempFileName);
        }
    }

    public static string GetText()
    {
        var tempFileName = Path.GetTempFileName();
        try
        {
            BashRunner.Run($"xclip -o > {tempFileName}");
            return File.ReadAllText(tempFileName);
        }
        finally
        {
            File.Delete(tempFileName);
        }
    }
}

static class BashRunner
{
    public static string Run(string commandLine)
    {
        var errorBuilder = new StringBuilder();
        var outputBuilder = new StringBuilder();
        var arguments = $"-c \"{commandLine}\"";
        using (var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "bash",
                Arguments = arguments,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = false,
            }
        })
        {
            process.Start();
            process.OutputDataReceived += (sender, args) => { outputBuilder.AppendLine(args.Data); };
            process.BeginOutputReadLine();
            process.ErrorDataReceived += (sender, args) => { errorBuilder.AppendLine(args.Data); };
            process.BeginErrorReadLine();
            if (!process.WaitForExit(500))
            {
                var timeoutError = $@"Process timed out. Command line: bash {arguments}.
Output: {outputBuilder}
Error: {errorBuilder}";
                throw new Exception(timeoutError);
            }
            if (process.ExitCode == 0)
            {
                return outputBuilder.ToString();
            }

            var error = $@"Could not execute process. Command line: bash {arguments}.
Output: {outputBuilder}
Error: {errorBuilder}";
            throw new Exception(error);
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.Net Core - 复制到剪贴板? 的相关文章

随机推荐

  • 如何使用 Python 获取触发我的 Azure 函数的 inputBlob 的名称

    我有一个天蓝色的函数 它是由放入 blob 存储的文件触发的 我想知道如何 如果可能 获取触发该函数的 blob 文件 的名称 我尝试这样做 fileObject os environ inputBlob message Python sc
  • 尝试启动 Atom/Nuclide 时未找到流

    我正在研究react native我正在使用的应用程序AtomIDE 我在打开时遇到错误Atom 我安装了babel cli and babel preset flow使用 npm 命令 npm install save dev babel
  • 将 MS Access 表单和类/模块递归导出到文本文件?

    我在一个古老的留言板上发现了一些代码 它很好地从类 模块和表单中导出了所有 VBA 代码 见下文 Option Explicit Option Compare Database Function SaveToFile Save the co
  • Java 8 流式添加两个或多个列表中的值

    我试图进入 Java 8 并了解流和 lambda 来解决各种问题 但陷入了这个特定的问题 我通常使用 forEach 并将值存储在 Map 中来解决 您将如何编写代码来使用 Java 8 中的新功能获取预期列表 List
  • 具有选择条件的 pandas 数据框中的高效最大选择

    我有一个带有列的 pandas 数据框 除其他外 user id and start time 我想高效且易读地找到与每个用户的最大值关联的所有行start time 例如 如果这是我的数据 user id start time A B C
  • 批量传输和 Android USB API

    我有一个程序 我尝试通过 USB 将我的 Android 设备连接到网络摄像头 我在一些事情上遇到了麻烦 即正确传输数据 我尝试过使用bulkTransfer 但似乎无法识别它的使用情况 我一直在尝试寻找可能对我有帮助的例子 例如here
  • 使用 Scala 生成代码

    在 Scala 中使用 SBT 工具链时 是否可以编写一个任务来读取项目源代码的特殊部分以在编译时生成 scala 代码 有什么想法甚至文章 教程吗 我正在寻找与 Template Haskell 非常相似的东西 环树者 scala htt
  • Laravel Lighthouse 中的身份验证

    在我的 graphql API 中 我必须通过两个不同的因素来授权对字段的请求 用户是否有权访问数据或者数据是否属于用户 例如 用户应该能够看到自己的用户数据 并且所有具有管理员权限的用户也应该能够看到这些数据 我想保护这些字段 以便具有不
  • RedisTemplate过期不起作用

    我正在尝试测试 RedisTemplate 中的过期方法 例如 我将会话存储在 redis 中 然后尝试检索会话并检查值是否相同 对于过期会话 我使用 redisTemplate 的 expire 方法 对于获取过期会话 我使用 getEx
  • NuGet System.Drawing.Common .NET 6 CA1416 此调用站点可在所有平台上访问。 “Image.FromStream(Stream)”仅支持:“windows”

    升级 NuGetSystem Drawing Common到 6 0 0 会导致以下错误 CA1416 此调用站点可在所有平台上访问 仅在 Windows 上支持 Image FromStream Stream https www nuge
  • 无法将整数类型转换为 uuid

    我试着跑python3 manage py migrate 但我得到了这个error File Library Frameworks Python framework Versions 3 8 lib python3 8 site pack
  • 特定于页面的 CSS 规则 - 将它们放在哪里?

    通常 当我设计网站时 我需要将特定样式应用于页面上的特定元素 并且我绝对确定它只会应用于该页面上的该元素 例如绝对定位的按钮 或者其他的东西 我不想诉诸内联样式 因为我倾向于同意样式与标记分开的理念 所以我发现自己在内部争论将样式定义放在哪
  • Pandas 重新映射到列中的范围

    我有一个带有 id s 列的 DataFrame 可以包含重复项 gt gt gt df user id head Out 3 0 2134 1 1234 2 4323 3 25434 4 1234 Name user id dtype i
  • Roxygen2 - 如何 @export 引用类生成器?

    例如 假设我有以下包称为Test我想导出课程A In R Test R docType package import methods exportClass A A lt setRefClass A methods list foo ide
  • ASP.NET 内存泄漏 - OracleCommand 对象

    我有内存泄漏 我很难找出问题出在哪里 ASP NET 进程时常会增加到 1GB 我已按照本页 http humblecoder co uk tag windbg 上的说明进行操作 并且 gcroot 命令返回以下内容 最后 x 行 我查看了
  • 将字符串格式化为标题大小写

    如何将字符串格式化为标题大小写 http www grammar monster com lessons capital letters title case htm 下面是一个在 C 中执行此操作的简单静态方法 public static
  • 如何在映射器(Hadoop)中使用MATLAB代码?

    我有一个处理图像的 matlab 代码 我想创建一个使用该代码的 Hadoop 映射器 我遇到了以下解决方案 但不确定哪一个最好 因为对我来说 在 hadoop 中的每个从属节点上安装 matlab 编译器运行时非常困难 手动将该 matl
  • SendKeys Ctrl + C 到外部应用程序(文本到剪贴板)

    我有一个应用程序作为托盘图标位于系统托盘中 我已经注册了一个热键 按下该热键将捕获任何应用程序中的当前文本选择 甚至在 Web 浏览器中也是如此 我的方法是发送组合键 Ctrl C 来复制文本 然后访问剪贴板并在我自己的应用程序中使用该文本
  • 如何使用 FlowLayout 确定 UICollectionView 的高度

    我有一个UICollectionView与UICollectionViewFlowLayout 我想计算它的内容大小 返回intrinsicContentSize通过自动布局调整其高度所需的 问题是 即使我对所有单元格都有固定且相等的高度
  • .Net Core - 复制到剪贴板?

    Is it possible to copy something to the clipboard using Net Core in a platform agnostic way 看来Clipboard https msdn micro