C# Remote application execution - c#

I'm trying to create some sort of remote application execution programs. The situation is that I have a script on a remote pc that I need to execute. Ideally I could do with being able to tell this remote pc to execute this script as though I was at it locally.
If possible it would be executable without the need to login to the pc (such as sending login details etc). I'm going for using .net c#.

The best code is code that you don't have to write yourself. Take a look at PSExec from Microsoft (SysInternals). Assuming that you have common credentials with the remote machine (either by virtue of a domain login or the same password and username), it should "Just Work".

Windows Management Instrumentation (WMI) allows you to do just that. If you need .NET bindings for it, they are in the System.Management namespace

I WOULD GO with what Rob or Daniel suggested, but if you want to do something dirty (don't) you could write a small service that you'd register on the remote PCs that would just watch for a specific trigger. For example, the service could periodically check a network drive for a file, a database for some parameters, etc.
But as I said previously, this is something I would do if I'd want something really temporary and wouldn't want to spend time to learn how to do it properly.

Related

How to properly execute C# file over local network?

Overview
C# File - Users PC
PHP Server - Hosts Webpages for application
Server and Users PC on local network
I have a c# file that reads weight from a USB scale. How would I trigger this file to run so it feeds into my program. The problem is I am using PHP to host our webpage/application so its not running client side and the scale is not hooked up to the server but to the clients PC.
The C# script would have to be on the clients in order to read the scale so how would I trigger this to happen?
Is this even possible and if not what would be a better way?
Important Edit
I was able to run the Scale Script (C#) when I wanted by having PHP and C# use TCP sockets.
The C# would listen for PHP to send something and when it did it would read the scale and send this information back to PHP becuase PHP was listening for a response. Mixed in with a little Ajax and it updates in the web browser.
Gave Chris Credit because he was the most helpful with answering my questions
It sounds like what you really want is for the client application to submit the data to the website itself, and the most suitable approach is probably to expose a web service from your server.
This service should accept weight data, along with some sort of customer key or whatever, to correlate the records correctly on the server side. I've never created a web service in PHP personally, so I can't give any advice on the implementation of that, but it is fairly trivial to hook a C# client app up to a web service once you've exposed its metadata (assuming you use SOAP).
you can't start C# application from a web page in a way that'll work in every browser every time. BUT, you can have some workarounds:
Use ActiveX component that read the data in the client and upload it to the server. the biggest cons is that it'll only work in Internet Explorer
use Silverlight client application that runs on elevated mode (v4) and upload the data to your server.
refer your clients to download application (the C# application you wrote about) and run it - this application will upload the data to your server.
hope this helps.
C# isn't a scripting language, it's a language that compiles into executable binaries or libraries. You won't be able to execute C# code on the client's computer via a website because C# code needs to be compiled before it can run.
Presumably what you really want is for your compiled C# binary to be executed on the client's machine via your website. You won't be able to easily do that. There are a lot of security measures in place to prevent browsers from running programs on your computer. There may be ways to hack around these security measures by using plugins (such as ActiveX), but it's not something that will be a one-liner.
Edit: I think you need to step back and think about what you're trying to do in a broad sense. You're trying to create a website that can read information from a user's USB port. This is the type of thing that browsers are designed to prevent, and for good reason. I wouldn't want random websites to be able to access peripheral hardware without my explicit permission. If you want this website to function the way you're expecting, you're going to have to seriously think about the security implications. You'll need some kind of client-side code (ActiveX, Silverlight, ...), and the user will need to explicitly give permission to for this all to happen. It won't be easy, and it won't be automatic. And I'm damn glad that's true.

Monitor running apps in .NET?

We have several apps that run on a Windows 2003/2008 server. Some of these apps are Java JAR files that are kicked off with a Scheduled Task using a app.cmd file.
Others are "big ones" like SQL Server and IIS.
I'd like to write an app (or service, actually) that simply monitors those programs and sees if they are running or not.
This is a little beyond what I've done before. Oh, this needs to be written in C#.
I was thinking of some type of "heart beat" pattern so that every few minutes, I check if the thread is running (again, new advanced threading) and if so, send out an "All's OK" message (using SMTP or something).
Any tips where to get started?
Thanks for any suggestions.
You can use Process.GetProcesses():
Use this method to create an array of new Process components and associate them with all the process resources on the local computer.
You should probably handle each application on a case-by-case basis, for exaple although you could just check for the existance of the SQL Server process you would be better off running a query that (for example) checks for the existance of a given database in the server.
Similarly you could run a simple HTTP request against an IIS server to check to make sure that the permissions are set up correctly etc...
Obviously the way that you test your Java processes would depend on what they do, although you could still just check for to see if the process is running (be aware that if they are running as a service they may be running inside one of the svchost.exe processes - this doesn't apply to scheduled tasks though).

Copy a file on a network path without transfering it to my own machine

If my .NET client uses System.IO.File.Copy to copy a file from \server1\share1\file1.txt to \sever1\share2\file2.txt, the file's data get read down to the client and then written back up to the server.
Is there an easy way to speed up things a bit by putting a process on the network-machine that realy does the copying?
I dont look for advice on programming such a service. I would rather find the tool or windows-functionality that already does this.
This sounds like a job for telnet or ssh, but these can be a pain to set up. I recommend you look at PsExec from microsoft which allows you to execute programs on a remote machine. You could simply use the copy program found in system32 through PsExec.
There is no need to create your own program to do this, just use the command line tools found on the target machine.
Almost certainly. I can think of a couple basic ways to do it:
Create a simple console applicaton that takes the source and destination paths/filenames, and performs a File.Copy(). Place it on the remote machine in a known location. Then invoke the process on the remote machine using PsExec. Your client app must be running in full trust in order to invoke a process programmatically, and the user running the app must have permission to run programs on the remote server.
Create a simple web method that again takes the source and destination and performs the copy. This requires setting up IIS on the network server with the requisite permissions to perform file access outside of the IIS "sandbox". However, it requires fewer client code permissions.
FYI CopyFileEx already does this if your client and server are Vista and later. So, not point in duplicating any of that effort in that case.
-scott

When do we use windows service?

Are there situations that we should use a windows service ?
I am building a client-server project (c#) and the server supposed to work alone without any user so someone advised me to use a windows service to run the server, is this right ? or there are a better solutions ?
Windows services are normally used when an application needs to continuously run. For example if they need to:
Wait for incoming requests. (Like through remoting or wcf)
Monitor a queue, file system etc.
If a program just needs to run periodically, like once a day. It is normally easier to create a scheduled task.
In your situation I would use a service for the following reasons:
You don't need to have a session running. This is good for security, and also reduces overhead on the server.
You get some of the managment commands built in for free
Start
Stop
Pause
Continue
You can handle server events such as shutdown.
Windows service can start running as soon as the machine is powered up, which makes ideal for running as a server, http server for example. No one is required to login.
You should create a Windows Service to run code in the background, without user interaction.
For example, a Windows Service will run even if no-one is logged on.
Any server that accepts connections (such as a mail, web, or FTP server) should usually be a Windows Service.
Well, a Windows Service provides a full framework for your application to work and to remain active while you want it to, so I think its ok.
Windows services are the right thing to use for something that should run all of the time, whether or not a user is logged in.
If you need something to run without an active user logged in, you need to use a windows service.
When you need the application to start running even when no one has physically logged into the machine, which is common with server machines, a service is a good candidate in this case. Especially because the service can be configured to auto start, which means the service will start when the machine is rebooted withut human intervention.
If however you are wanting to host web services (WCF) while a service is an option, you might consider hosting in IIS, this relieves you of writing the actual hosting code etc.

Novell Netware 6 and C#

I don't know if anyone has any expertise with Novell Netware or not, but I situation where one of my clients is getting a weird error trying to move a file to a netware share. The situation is pretty complicated but I'll try and outline it as best I can.
1) The application is running as a service
2) The file to be moved is a local file in an accessible location (to the service)
3) The file is being moved with FileInfo.MoveTo().
4) The MoveTo is throwing an IOException and the message is "a device attached to the system is not functioning".
I'm at a loss as to explain what might be happening. On a side note, it seems that running a small test program interactively using the same method (FileInfo.MoveTo) to the same location works fine so it seems to be associated with the service itself.
Any help would be hugely greatful. Thanks.
Kristofor Wilson
That's normal. Windows services need to login to the server separately, like local users do. When you run your program interactively, it accesses the NetWare server using your, already established, connection. To access the server from the service, your program needs to establish its own connection first.
I am afraid I don't know how to login to NetWare from a C# program, and I am not sure if it is possible to set up your service that it does not need to log in to NetWare. I'd explore one of these ways though.
Oh, almost forgot. If a local user has NetWare volumes mapped to disk drive letters in Windows, like SYS: volume to P: and so on, these mapping will not be visible to the service; it will have to map the volumes to letters separately, or use UNC paths
The later (Last versions) of Netware where able to emulate a windows server whilst still functioning as a Netware server. No big deal to set this up so might be an option for you,.

Categories

Resources