How do I keep a process open? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a web application in ASP.Net/C# (using MVS), and I want to be able to launch some programs on the client side (this should be safe since it's only for intranet use), and keep these programs open in order to give them some command inputs through the web app.
For example, I would like to open "cmd.exe" client-side, and then being able to send multiple commands one after another (synchronized with some buttons of my web form) to the process.
How can I do this? I've read a lot about using a block of Javascript with an ActiveX object, or in C# with System.Diagnostics.Process, but I'm quite stuck on how to proceed.

function RunEXE()
{
var oShell = new ActiveXObject("WScript.Shell");
var prog = "c:\\WINDOWS\\system32\\notepad.exe";
oShell.Run('"'+prog+'"',1);
}

You could also have a look at custom protocol handlers, that's what e.g. Steam uses. You'd simply have a link like myprotocol://dosomething/whatever and it will launch your client-side application with the given URL.
It's basically just about writing a registry key: http://msdn.microsoft.com/en-us/library/ie/aa767914(v=vs.85).aspx
For example, to register a protocol named alert, you can do this:
HKEY_CLASSES_ROOT
alert
(Default) = "URL:Alert Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "alert.exe,1"
shell
open
command
(Default) = "C:\Program Files\Alert\alert.exe" "%1"
This will cause the application in command to be launched when you navigate to an url that starts with alert://.

Using c#
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "CMD.EXE";
startInfo.Arguments = "list of args";
Process.Start(startInfo);
Now after that you can keep checking weather the is open or not by
Process[] processes = Process.GetProcesses();
or
Process[] chromes = Process.GetProcessesByName("chrome");
Check if you process is open or not then pass args to it

Related

C# Get currently logged in users logged in time in .net forms application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to get the Windows login time for the current user.
I am using a Windows .NET Form. Nothing I've found works. The time that I end up getting is the LAST login time and not the latest login time which makes no sense. Seems to me that Microsoft broke this function but what do i know.
I have already tried all methods on this page and still not the right times. I started a new thread as to not resurrect a dead thread but I may be wrong for doing this. Sorry if that is the case.
The current user is
System.Security.Principal.WindowsIdentity.GetCurrent()
But there is no Login-Time stored, and your approach sounds strange.
If someone starts your Application 5 days after Windows was started, you want to logout the user from windows, because he logged in more then 45min ago ? I hope I misunderstand this.
You just have to track the time your application is running. It's a single-user application, you can shut down your application at any time, if you like.
I can't comment yet so I have to propose this as an answer.
Have you tried running the tool wiser in the manner suggested here?
Run Command Prompt Commands
Inside here you can capture the output and parse it.
private List<string> RunCommand(string exeName, string args, string folder)
{
ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = folder;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = args;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
var results = new List<string>();
while (!process.StandardOutput.EndOfStream)
{
results.Add(process.StandardOutput.ReadLine());
}
return results;
}
Above is the code I use.
Apologies for formatting as I am doing this on my phone

How to run a .bat file from a .exe application in a different folder? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an .exe application that opens another .exe application in a different folder, which then executes a .bat file to compile a .tex document.
If the initial .exe application is in a different file to the .tex document it can not find it, but if it is in the same folder it runs perfectly.
Any way I can solve this issue? I need to be able to run the initial .exe from a different folder.
If you are calling batch file using Process class, dont forget to set WorkingDirectory property, otherwise it will use your executable` location as default path.
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = #"D:\Dir\Run.bat";
process.StartInfo.WorkingDirectory = #"D:\Dir";
process.Start();
If you use System.Diagnostics.Process.Start you can specify the folder in which to run the batch file.
System.Diagnosis.Process

Read text from console C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a question that i could not find on the internet.
For i friend of mine i am making a application that wil work after a command. Pretty easy but here comes the tricky part. That command is given in a other console. So what i am trying to do is read the lines from the other console and that should trigger an action in mine to write to the other console.
Like i said i've tried to find something on the internet that can help me but i could not find anything.
Is this possible and if it is possible how can i accomplish this?
Thanks in advance
EDIT
The other console is an gameserver that is started with an batch file
I could trigger the gameserver from my own application, but it will take over the program so how can i make it that it triggers in my program and then read the lines from the program
Since you want to control the input of the gameserver, you should start the gameserver from your application, so that you get control over the standard input of the gameserver.
Then you read all the lines from standard input, and check them before writing them to the gameserver's standard input. That way you can also write extra lines to the gameserver's standard input.
Code from http://social.msdn.microsoft.com/forums/vstudio/en-US/46c91711-755f-48fa-a4a8-92956082218f/howto-launch-process-and-write-to-its-stdin:
using System.Diagnostics;
...
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false; //required to redirect standart input/output
// redirects on your choice
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = ...app path to execute...;
startInfo.Arguments = ...argumetns if required...;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine(...write whatever you want...);

How to get the path of the installed Excel.exe programmatically? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there any way that I could find a way to retrieve the path of the Excel executable file in C# or VB.Net? I'm aware of these two options below, but unfortunately they are not suitable. Please suggest if you have an alternative.
Using the Registry
Using Process.Start("Excel") and getting the path from the Excel process
Cannot use the Registry option because people without sufficient rights may be using my application and hence we wanted to stay away. If this assumption is wrong and Registry is reliable for any level of user access rights, please let me know.
With Second option, I'm seeing the Excel process being opened, if there is a way where I can start, but not have Excel process appear at all to the user, this may work.
I ended up with this logic and it serves my purpose. If Excel is a running process, then I'm getting the path from there, if not I'm getting by creating an instance, which doesn't actually show the Excel UI, but runs in the background.
string path = string.Empty;
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == "EXCEL")
{
path = theprocess.MainModule.FileName;
break;
}
}
if (path == string.Empty)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
dynamic xlApp = Activator.CreateInstance(officeType);
xlApp.Visible = false;
path = xlApp.Path + #"\Excel.exe";
xlApp.Quit();
}
You can start it hidden and close it. You would have to include the interop services for the below code
Dim xl As Excel.Application = New Excel.Application
xl.Visible = False
Dim Excel_Path As String = xl.Path()
xl.Quit()
Check at this link
You can start the process hidden, so the user will not see the window
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

call a console application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the code to call a console application within a WPF? I have an application in WPF you need:
call a console application;
the console application closes the WPF
The console application again calls WPF and close console application.
This is necessary because I am doing a system where updates to the application must close the files to copy.
For close application WPF I am using the following:
Process wpfProc = Process.GetProcessesByName("MainWindow.exe").First();
wpfProc.Kill();
return in console application:
Unhandled Exception: System.InvalidOperationException: Sequence sontains no elements
at System.Link.Enumerable.First[TSource]<IEnumerable'1 source>
at Updater.Program.Main<String[] args> in d:\endereçodoUpdater\Program.cs:line 17
in line 17 have the following:
Process wpfProc = Process.GetProcessesByName("MainWindow").First();
How do I resolve this?
You do this with Process.Start:
Process myProc = Process.Start("MyConsoleApp.exe");
//Close gracefully
Application.Exit();
In MyConsoleApp.exe, you would need to use GetProcessByName to kill your WPF app, and then Process.Start again to restart it:
Process wpfProc = Process.GetProcessesByName("MyWpfApp.exe").First();
//If you want to directly kill it
wpfProc.Kill();
//Or be nice and let it kill itself
wpfProc.WaitForExit();
//Do stuff
Process.Start("MyWpfApp.exe");
System.Diagnostics.Process on MSDN

Categories