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();

        [STAThread]
        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?
 
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();
            runspace.Open();
            using (var ps = PowerShell.Create())
            {
                ps.Runspace = runspace;
                var server = "servername";
                ps.AddScript($"$a= New-PSSession {server}");
                ps.Invoke();
                ps.AddScript($"Copy-Item -ToSession $a -Path {source} -Destination {Dest}");
                ps.Invoke();
            }
            runspace.Close();

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...
0
 
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.
0
 
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 - so....it's another late night.

If you have any suggestions - I'm all ears - thanks again.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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?
0
 
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.
0
 
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.

https://msdn.microsoft.com/en-us/library/aa479405.aspx

Users could then browse to the URL and upload their files from there.  Could authenticate against AD for extra security.
0
 
Andy BrownDeveloperAuthor Commented:
Fantastic - that looks like it is going to do the trick perfectly.  Thank you so much.
0
 
Andy BrownDeveloperAuthor Commented:
Thanks for your suggestion Kyle - I might look at that at some point.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.