almas
almas

Reputation: 34

How to use Clipboard in C# services?

  1. Keep in mind i am using services not Windows Form App.
  2. When I try to use System.Windows.Forms.Clipboard.GetText(); my code don't go to next step.
  3. means System.Windows.Forms Controls are deprecated
  4. I am Trying to get copied text from Clipboard and change the text then set back to clipboard.
  5. Please Help me.

below is my code

 public void working()
    {
        while (true)
        {

            // Get text from clipboard 
             string clipboardtext = System.Windows.Forms.Clipboard.GetText();

            string path = "c:\\sample.txt";
            using (StreamWriter writer = new StreamWriter(path, true))
            {
                
                writer.WriteLine(string.Format("Clip Board Test " + "-" + " " + "Windows Service is called on " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss") + ""));
                writer.Close();
                System.Windows.Forms.Clipboard.SetDataObject("Hello ClipBoard");
            }
            Thread.Sleep(ScheduleTime * 6000);
        }

    }

Upvotes: 0

Views: 580

Answers (1)

Vít Bednář
Vít Bednář

Reputation: 318

Windows service is not running in same "scope" as your user GUI, it cannot access to your clipboard or key stroke or anything you do. Services are useful, because they can run without logged user or under different user.

Services are not allowed to access your keystrokes or clipboard also for security reasons.

Upvotes: 2

Related Questions