i have build script in remote machine.but i want to start the build from my local machine.so for this i need to update the input.properties file in remote machine and then run the batch file to start the build process. For this i have created one web page
so how can i modify the remote input.properties file and run the batch file in C#.
please give me some suggestion for this.
thanks in advance...
You need to edit the properties file remotely, using a Stream Reader / Stream Writer. There are many ways to do that. You should be able to solve that yourself.
Once you are happy that the properties file is updated correctly you will need to use PSEXEC service to launch the Batch file locally on the User's machine. So in this case, the batch file needs to be copied over to the remote machine.
What I normally do is write the Batch file your intending on using to the remote machine on the fly, and as soon as I can see that the associated process has finished, I remove the batch file from the local machine.
PSEXEC will be ran at your end, you use it to connect up to the end users machine and fire the batch file.
You can create a process like below :-
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\psexec.exe";
p.StartInfo.Arguments = "\\\\" + computerName + " C:\\YourBatFile.bat";
p.Start();
p.WaitForExit();
This will open a process on the local machine, that will launch your batch file.
I would recommend learning about the PSEXEC Service, its how I've always launched remote processed.
Hope this helps.
Related
In my project I need to start a batch file from a thread on the server and wait for it to finish. My code is:
var process = new Process();
process.StartInfo.FileName = batchFilePath;
process.StartInfo.Arguments = "-0";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();
and in the batch is simply put "pause".
The problem is that is don't see any window open and the thread is stuck and wait for the batch to finish.Only when I close the process by the windows task manager the program continues. The problem happens also if I put in the batch "cmd". How can I see the window? I want to make sure that the batch is running correctly.
This is a batch file running on the server? And you are not logged into the server, but into a web client?
You won't be able to see the console window from a web client, because it's running on a different machine.
If you are logged into the server, but still aren't seeing the window, you could try researching and experimenting with the ProcessStartInfo properties CreateNoWindow or UseShellExecute.
You might also want to look into RedirectStandardInput and RedirectStandardOutput if you need to see (or inspect programmatically) what the process writes to the console window.
I have developed a c# application to download a zip file from a site, when manually run the app, it does its job normally and exits, But when i schedule it to run automatically using task scheduler in Windows, it throws web client exception error. Please help me regarding this
The part of code for downloading the file is
WebClient wc = new WebClient();
wc.DownloadFile(<site>, "feed.zip");
System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo("extract.bat");
System.Diagnostics.Process process = new Process();
pinfo.CreateNoWindow = true;
pinfo.WorkingDirectory = Directory.GetCurrentDirectory();
process.StartInfo = pinfo;
process.Start();
process.WaitForExit();
The batch file extracts the zip file.
I see these possible problems:
If you run your application Directory.GetCurrentDirectory() could result in a different path. Directory.GetCurrentDirectory() will be set to your bin-folder, but in your production environment, it can point every, depending how your process is executed (for example: if you create a shortcut on your desktop, you can also modify the Start in-folder.
If your current directory is configured wrong, the application would also not be able to find extract.bat.
While you are debugging you have some rights on your outputfolder. But when your application is executed as a batch process, it runs under a different account. Does this account have to rights to write to your output folder?
The website your are accessing might require a valid account. You possibly have a valid account. But the account your application is running from in production, might not be valid account for that website.
You are possibly behind a proxy. Perhaps you need to configure some extra settings for getting past that proxy. In your account, these settings are configured inside Internet Explorer. Perhaps the production account does not have these settings configured.
I am developing an app that should be portable and I am using mongodb.
By portable I means that my app has a folder with all: dlls, exes, mongo files, mongo databases. Then with this folder I can run my app in any machine.
Then I need to know:
Is there some library that allow me to run the mongod process when the app start and end
the process when the app ends ?
Exists a good practice to do that stuff ?
Advices are welcome and thanks in advance.
According to the MongoDb installation instructions it should be quite simple.
Mongodb starts as a console application waiting for connections, so when your app starts, you should run mongodb hidden. We are always assuming that ALL the mongodb files are in place with your application files and database files are in the correct directory).
When your app terminates, you should kill the process.
Yo should set the correct paths on this example:
//starting the mongod server (when app starts)
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = dir + #"\mongod.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.Arguments = "--dbpath d:\test\mongodb\data";
Process mongod = Process.Start(start);
//stopping the mongod server (when app is closing)
mongod.Kill();
You can see more information about the mongod configuration and running here
I needed to do the same thing and my starting point was Salvador Sarpi's answer. But, I found a couple things that needed to be added to his example.
First, you need to set UseShellExecute to false for the ProcessStartInfo object. Otherwise, you may get a security warning when the process is started asking the user if they want to run it or not. I don't think this is desired.
Second, you need to call Shutdown on the MongoServer object before killing the process. I had an issue where it locked the database and required it to be repaired if I didn't call the Shutdown method before killing the process. See Here for details on repairing
My final code is different, but for this example I used Salvador's code as the base for reference.
//starting the mongod server (when app starts)
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = dir + #"\mongod.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
// set UseShellExecute to false
start.UseShellExecute = false;
//#"" prevents need for backslashes
start.Arguments = #"--dbpath d:\test\mongodb\data";
Process mongod = Process.Start(start);
// Mongo CSharp Driver Code (see Mongo docs)
MongoClient client = new MongoClient();
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase("Database_Name_Here");
// Doing awesome stuff here ...
// Shutdown Server when done.
server.Shutdown();
//stopping the mongod server (when app is closing)
mongod.Kill();
I have an exe which I call from the command line. Is it possible to execute that file on the server? On the computer if the file is located in the folder abc, I go to folder abc and than I execute the batch. Hw do I do this in C#
Code example below, make sure you have your permissions setup correctly:
System.Diagnostics.Process yourProcess = new System.Diagnostics.Process();
// Set the directory
yourProcess.StartInfo.WorkingDirectory = Request.MapPath("~/"); //or wherever your file is
// Set the filename
yourProcess.StartInfo.FileName = Request.MapPath("bla.exe");
// Start the process
yourProcess.Start();
ASP Net - Run Application (EXE) from ASP.Net C#
In server side code certainly, Process.Start(MyExeFile) will do that but, as long as the user account you are running your stuff on can execute it.
I need to call a console application to load data into another desktop application on the remote server that located within the corporate domain.
Users will enter the web page and upload data to asp.net web server, which after transformation should call that console application. Users are located remotely and do not have any other access except the web server.
I decided to lower the security web application context and let the asp.net working process to start the console application on the current IIS 6.0 web server
What I have done:
I changed the security account for the application pool for Local System;
I added ASPNET Account and IIS_WPG IIS Process Account to Administrators group;
I added “Allow service to interact with desctop” for “IIS Admin Service” and “World Wide Web Publishing Service” processes and restarted the machine;
I tried to start BAT-file at server side through the test page code-behind, but failed:
protected void btnStart_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = #”C:\run.bat”;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
The error was access denied.
Please help me to find any workable idea how to start the bat-file at web server side.
Thanks
Try setting UseShellExecute to true instead of false. After all, batch files run in a shell - so you need a shell to execute it. (Another option is to run cmd.exe and pass the name of the batch file in as an argument, e.g. "cmd.exe /k c:\run.bat")
You might also want to try creating a simple .NET app which just (say) creates a file with a timestamp in. That way you can test the "can I start another process" bit separately from the "can I get the batch file to work" bit.
Put that particular batch file in your application itself.
string str_Path = Server.MapPath(".") + "\\run.bat";
ProcessStartInfo processInfo = new ProcessStartInfo(str_Path);
processInfo.UseShellExecute = false;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.Start();
Take a look at this example: Run Interactive Command Shell or Batch Files From ASP.NET
It uses little different approach. They suggest running cmd.exe and executing command line by line.