If I try to open Notepad from a .NET console application it works fine.
I'm doing it as follows:
var p = new Process
{
StartInfo =
{
FileName = "c:\windows\system32\notepad.exe";
}
};
p.Start();
When I try to open the application I actually want to open, nothing happens. If I open that application by hand I see a Java process being created, which means it's a Java application packaged as an exe file.
Any ideas on how to open Java exe apps through .NET?
Thanks in advance
There shouldn't be much of a difference between a regular EXE and an "exified" Java application. Have you tried adjusting the working directory? Maybe there's some unzipping going on.
Lets say you had an application named "HelloWorld". Then from a command prompt (and from your code) you would launch it with:
java HelloWorld
Now this would assume that java is in your path. Which is a bad assumption. So you would be better off either having logic to populate the path to java, or to hardcode it.
I don't ever recommend hardcoding a path if you plan to pass this software around. It's never a sure thing...
Related
I have made a very simple WPF application that is intended to have a couple of options:
Option 1 is to launch a PDF file
Option 2 is to launch a couple of exe files needed to run my software.
The problem is that on the machine I have developed this on it does open both .exe files just fine, but the strange thig is that when I try this on my HP Laptop it only wants to open the unity3d exe file and not the Voice Server exe file.
Does anybody have any idea why this is the case, it's driving me nuts trying to figure it out. here is how I have by button call coded in C#
public static void LaunchKOS()
{
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var desktopPathkos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// Launch Voice Server
var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");
Process.Start(combinedPath);
// Launch Knight O.S. Unity Settings Window
var combinedPathkos = Path.Combine(desktopPathkos, "Knight O S Beta01");
Process.Start(combinedPathkos);
// Close This Window
Environment.Exit(0);
}
EDIT:
On the outside chance that for some reason it did not like doing both .exe files from one button I have even tried just launching the Voiceserver.exe from the one button call and I still get the same result where it will launch it on the machine I have developed the app on but not on any other machine despite the fact that the file folders are exactly the same??
There has to be some logical reason for why this is not working??
Anyone?!?
EDIT: More than one way to skin a cat
Well, it may not be the most elegant way to have solved the problem but I've managed to come up with a work-around.
What I did was create a shortcut of the VoiceServer.exe and then in my button code execute the shortcut and now it will open both .exe files.
It's beyond stupid that I should even have to do that, but that's what I ended up doing. :/
The folder separator for local files paths is "\" rather than "/".
Path.Combine() is defined with a params parameter, so to avoid issues with \ being treated as an escape character, it's cleaner to just pass each folder name as a separate string.
var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data", "voice-recognition-server-pc", "VoiceServer", "bin", "Release", "KittVoiceServer");
So i'm trying to write a program that will list all the installed programs on Windows and let user decided which one to be blocked(Killed).
I found the solution of List Installed program here. Get installed applications in a system
And to kill a program you just need the .exe name and kill all the process with that name.
But my problem is the names in the list of installed program is not necccesary the .exe name, and you couldn't find a process without the .exe name. For example, the installed program name is "Google Chrome" but the .exe or the process name would be chrome.
The current solution I can think of is to find out the installed directory of the installed program, find all the .exe files under it. And kill all of them if required. But I don't know how to get the directory of all the installed programs.
So, I can't help wonder, is there a more elegant of doing it. Any suggestions would be appreciated.
You have to split this question on 2 parts:
1. Find all the programs
What does it mean? It means that You want to find all the executable files (*.exe) on the hard drive of the client. This executable files does include the code which will run in the memory and do some work.
BUT: The code located on harddisk cannot be "killed", as the code (exe file) will start first new instance of application (via OS interface). Once You got the instance we can start talking about other part of question.
Also as stated there, You are unable to get easily 1 exe file, as multiple application might have multiple exe files. And if You really want to have them, You got to run analysis on the harddisk, like there:
var allExePaths =
from drive in Environment.GetLogicalDrives()
from exePath in Directory.GetFiles(drive, "*.exe", SearchOption.AllDirectories)
select exePath;
Also as Ivar stated in comments, there are multiple executables, like *.jar, *.pyc, ... which might be run via other executable file.
2. Kill the required program
If You want to kill some program/application, You need to get it's instance. Instance are distinguished by PID (process ID). Once You got this of the instance, You can kill it. Answer for this is there: How do I kill a process using Vb.NET or C#?
The code below will take all process with particular name and kill them all.
Code:
foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )
{
try
{
p.Kill();
p.WaitForExit(); // possibly with a timeout
}
catch ( Win32Exception winException )
{
// process was terminating or can't be terminated - deal with it
}
catch ( InvalidOperationException invalidException )
{
// process has already exited - might be able to let this one go
}
}
I would like to open programmaticaly an application.
First i used System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Program1.exe") works fine, but the application always needs to be on the same path (not always true because different computer)
And a simple way to find it (with mouse & click, not programmaticaly though) is to use the windows file explorer, i enter the title of my application and i find it instantly.
I would like to code that.
I thought i could use the keyboard shortcut "Home + F" and simulate the word with SendKeys.Send("blabla") but the "Home key" doesn't seem to exist with c# (at least not here https://msdn.microsoft.com/fr-fr/library/ms127847(v=vs.110).aspx)
It's a little program for children i can't expect them to find manually the path (so forget the OpenFileDialog..)
Maybe thats a very bad idea and there are another solution to find a program without knowing his path, i don't know have you got better idea?!
You're trying to solve the problem the wrong way. What you're trying to "program" is the setting oft the working directory when the program is run using Explorer. The right way to do that is to use Process.Start by passing it all of the necessary info to start the process:
var startInfo = new ProcessStartInfo("Program1.exe");
startInfo.WorkingDirectory = #"C:\Program Files (x86)\";
Process proc = Process.Start(startInfo);
I want to develop an application, App1, that gets some information & settings from the user. Then, using these settings, it should build a second application, App2. I want to have App1 build App2's exe file.
I know that one way to do this is to make a text or XML file to hold the settings and put this next to App2's exe file, but I want to embed these settings into App2's exe file instead. How can I do this using Visual Studio, the .net framework, and the C# language?
I don't understand what do you mean by those user information and settings (some example would help), but basically, what you want to do is invoke the C# compiler as follows
var p = new Process();
p.StartInfo.FileName = #"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe";
p.StartInfo.Arguments = #"c:\aa\Test.cs";
p.Start();
These four lines invoke the c# compiler on a C# code file and produces an exe file in the output folder for your project.
MSDN provides more info on how to work with the compiler via command line. Especially, how to compile more than one file, how to compile a library, etc.
You can accomplish the same thing like this:
Make two applications. App2 will be the same .exe file for all cases of App1. Instead of building an .exe file, App1 will generate a configuration file containing the information and settings input by the user. This file will be read by App2. App2 will then call the appropriate functions within App2, based on the information and settings in the configuration file.
I'm trying to do the same thing for the same reasons, but using VB.NET. That's how I found this question. The only way I can thing of (other than editing the source code each time, which is what I'll be doing unless I find a better way), is to create a second program that encrypts a parameter file and give the encrypted parameter file and the exe file to the end user.
I'm still using the "editing the source method" because I want to give the user just the exe file without dealing with parameter files or encryption. There aren't many users who will need this in my case (for the moment) so I can deal with that method for now.
I am looking for resources that have examples for creating a Console Application. I am past the "Hello World" stage but am stumped at the point where I need to run an application. I have the string that I need to run that I pulled from the batch file that I am trying to automate in a C# app. I need help with knowing which classes and namespaces have the functionality I need to run it.
Edit: Sorry for the poorly asked question. I'll rewrite it.
I am trying to create a console application that will replace a batch file that I have partially written. Some of the data and file manipulations that I need to do are more complex than can easily be done in a batch file. So far I am reading, writing, and manipulating the files just fine. I am having difficulty when trying figure out how to run a command to execute an application on the server with the proper arguments being passed in.
Update: a coworker gave me the following code snippit which is exactly what I needed to move forward. I am sorry the question was worded so badly.
public static string MyDOSMethod()
{
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
p.StandardInput.WriteLine(#"cd \windows\system32");
p.StandardInput.WriteLine("exit");
try
{
return p.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
return e.Message;
}
}
The question is not perfectly clear, what I understood is "I need to start an application from my console application; what class can I use?"
My answer is: you should have a look at the static method Process.Start (and in general at the class Process of namespace System.Diagnostics).
Have a look at this tutorial, it will guide you through the usage of Process.Start and ProcessStartInfo (which runs a process and gives you feedback)
I recommend this introduction to C# if your new to programming.
http://www.csharp-station.com/Tutorial.aspx
In order to compile you need a compiler and a GUI to edit code in is also nice :o) Here you can use the free version of Visual Studio:
http://www.microsoft.com/express/
In Visual Studio just click new select console application and i think you will get a "default application" like Hello World that you can run and build.