I tried to write a very small app for manipulating the system clipboard. The class used is System.Windows.Clipboard
.
When I write it with VS template of Console app, it does not work, reporting: "The name 'DataFormats' does not exist in the current context".
(code in Program.cs)
using System;
using System.Windows;
namespace clipboard_console
{
public class Program
{
static void Main()
{
string textData = "XYZ";
DataObject data = new DataObject(DataFormats.UnicodeText, (Object)textData, true);
Clipboard.SetDataObject(data, true);
}
}
}
To make it work, I need to use the VS template of WPF app, but running the app would popup a blank window.
(code in MainWindow.xaml.cs)
using System;
using System.Windows;
namespace clipboard_wpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
string textData = "XYZ";
DataObject data = new DataObject(DataFormats.UnicodeText, (Object)textData, true);
Clipboard.SetDataObject(data, true);
}
}
}
How can I get an app that quiely changes the clipboard content only? Thank you!
Reply to Answer no.1
Hi @digital_jedi Thanks for your solution!
My default project file is like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
I follow your recommendations to change it to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Now the references seem to be ok. But running it lead to the error message:
Clipboard is available in System.Windows
namespace (PresentationCore.dll included in WPF apps) and also System.Windows.Forms
namespace (System.Windows.Forms.dll included in WinForms apps). You can reference either from your console project by doing the following (example for net 6):
PresentationCore.dll:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
System.Windows.Forms.dll:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Note the -windows
suffix on the TargetFramework
, and the usage of <UseWPF>true</UseWPF>
or <UseWindowsForms>true</UseWindowsForms>
. You could also reference both like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
EDIT:
Given the issues with WPF regarding hiding the window and trying to shutdown the exe afterwards, I recommend using the System.Windows.Forms
clipboard instead of WPF.
Setup reference like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Main.cs:
using System;
using System.Windows.Forms;
namespace ClipboardApp
{
public class Program
{
[STAThread]
public static void Main()
{
string textData = "XYZ";
DataObject data = new DataObject(DataFormats.UnicodeText, textData);
Clipboard.SetDataObject(data, true);
}
}
}