0

Hello everyone and thank you in advance.

I have copied a text and now I want to paste this text into a ".txt" what needs to be created automatically. I know the paste simulation key is as follows:

System.Windows.Forms.SendKeys.Send("^{v}");

On the other hand, the previous simulation key press should be included somehow within the following code (which creates a writes), but I am not sure how to do this...

public void writeTXT()
{
    if (!File.Exists(path))
    {
        using (StreamWriter sw = File.CreateText(path))
        {
           sw.WriteLine();
        }
     }
}

Any comments will be welcome! Thanks a lot.

SciPhy
  • 143
  • 1
  • 1
  • 10
  • Take a look at this : https://stackoverflow.com/questions/15292175/c-sharp-using-sendkey-function-to-send-a-key-to-another-application – Grimson Sep 14 '18 at 10:00
  • 1
    Are you looking for [this](https://learn.microsoft.com/en-gb/dotnet/api/system.windows.clipboard?redirectedfrom=MSDN&view=netframework-4.7.2) ? – Cid Sep 14 '18 at 10:00
  • 4
    `Clipboard.GetText()`. There's no need to futz around with simulated keypresses at all. – Jeroen Mostert Sep 14 '18 at 10:00
  • I didnt know the Clipboard class, thank you very much! – SciPhy Sep 14 '18 at 10:02

2 Answers2

3

You don't have to simulate keypresses. You can access the clipboard directly:

using (StreamWriter sw = File.CreateText(path))
{
   sw.WriteLine(Clipboard.GetText());
}
Sefe
  • 13,731
  • 5
  • 42
  • 55
-1

You don't need to simulate a Ctrl+V.

Use the Clipboard class instead.

You can find info in there :

https://learn.microsoft.com/fr-fr/dotnet/api/system.windows.clipboard?redirectedfrom=MSDN&view=netframework-4.7.2

Cesar
  • 453
  • 9
  • 21