Pasting files from the local PC to a Remote desktop (via C#)

With a lot of help, I have written a small c# app that will paste the contents of the clipboard to a specified folder.  Sadly, however, when I put the .exe file onto my RDP - it doesn't work (I'm guessing because it's looking at the RDP clipboard).  Any pointers (or nuget suggestions), would be greatly appreciated - here's the code that I've been working with:

using System;
using System.IO;
using System.Windows.Forms;

namespace TSPaste2
    class Program
        //Getting destination foler
        String[] args = Environment.GetCommandLineArgs();

        static void Main(string[] args)
            //Setting Destination foler
            string DestFolder = args[0];

            if (Clipboard.ContainsFileDropList())
                //copy to folder as set in the Command line
                foreach (string source in Clipboard.GetFileDropList())
                    string Dest = DestFolder + "\\" + Path.GetFileName(source);
                    File.Copy(source, Dest);

Open in new window

Andy BrownDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Hi Andy,

Can you be more clear about your use case?  EG: you have files on your local machine that you're trying to copy up to the remote desktop?

In that case the exe would go on your local machine and I would actually copy to a shared drive on the server you're RDPing to.
Andy BrownDeveloperAuthor Commented:
Hi Kyle - I've been trying all sorts - and each time I get close, something stumps me...

We have several locked down VM / RDP servers, where users login.  We need to provide users with the ability to copy files from their local PC and paste them into a specific folder, which our application will then process the files.  

If we use Explorer to paste it works fine (but gives us a security headache).  If I create a C# app that displays a restricted Explorer dialog window, it almost works, but gives me other headaches.

My preferred method is to do everything in the background.  In theory, all I needed to do ispaste the contents of the local clipboard into a specifc folder - hence this approach.  

In truth, I don't care how it does it - but it needs to be reliable and secure.  The main application running on the server will do the necessary checks and delete anything it doesn't want.  

I'm now about to look into scripts and something called AutoIT -'s another late night.

If you have any suggestions - I'm all ears - thanks again.
Kyle AbrahamsSenior .Net DeveloperCommented:
The only way to get access to the files on the local pc is to have the program run on the local pc (or access them over the network share via an admin account).

From there you could copy the files via your code to the share and let the server process from there.

If there were security concerns you could also do the following:
1)  Hide the share on the server with a $  (EG: create  a new share called copiedfiles$)  This would allow you to access the share but not have it presented when typing "\\server\"

2)  You could impersonate mapping the drive via the program.
          EG:  Let the program have a hard coded (or look up in a database / webservice) username / password that has access to the share (eg: a system account) that normal users don't have.  Map the drive, copy the files, delete the map.  Note if you're going the webservice route I would recommend you encrypt the text and decrypt it in the program as someone could always call the webservice directly.

Either way, the program would need to run on the local computer, then copy up to the share.  

Another alternative would be to go the FTP route, but same situation, users need to have knowledge about how to do that.

I guess first question is can you run the program on the local computer?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Andy BrownDeveloperAuthor Commented:
Thanks Kyle - It's a bit tricky, as I really need to keep everything running on the server.  Also, setting up shares on the RDP to the local PCs would be a difficult sell to the end-user (company).

I was thinking of looking at something like AutoIT or possibly Powershell (running on the server to Open folder, Paste clipboard, Close), but I'm currently leaning towards my c# Explorer app that certainly gets the files where they need to be - with ease, but I'm having a few niggles with the WebBrowser object that I'm using.
Kyle AbrahamsSenior .Net DeveloperCommented:
What about creating a web application that will allow uploads?  EG:  You can use the web app to upload the file to the target directory?

You wouldn't need to do a webbrowser application for that.  Just run it in IIS on the server.

Users could then browse to the URL and upload their files from there.  Could authenticate against AD for extra security.
Jayadev NairApplication DeveloperCommented:
If your server's management service is allowing you to connect with powershell console, then here you go,

Add the reference of "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll" to your console application and replace following code on your line 24.

var runspace = RunspaceFactory.CreateRunspace();
            using (var ps = PowerShell.Create())
                ps.Runspace = runspace;
                var server = "servername";
                ps.AddScript($"$a= New-PSSession {server}");
                ps.AddScript($"Copy-Item -ToSession $a -Path {source} -Destination {Dest}");

Open in new window

Edit: If your server requires authentication, consider adding -Credentials in my line 7 (only if required)
Edit2: And again, your app should run on Client desktops, not on server since you need Clipboard to be accessible locally

It works for me...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andy BrownDeveloperAuthor Commented:
Fantastic - that looks like it is going to do the trick perfectly.  Thank you so much.
Andy BrownDeveloperAuthor Commented:
Thanks for your suggestion Kyle - I might look at that at some point.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.