Read text from console C# [closed] - c#

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

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 do I keep a process open? [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 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

Using System.Diagnostic.Process.Start and assigning parameters in outside .exe file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm relatively new at C# development and I'm trying to figure out how I can use an outside application to accept parameters set in my VS (2008) project. The idea is to have the outside application accept coordinates to rotate the screen. So have it rotate to 90 degrees, 180 degrees, and 270 degrees. So I have two buttons. One to send the command to rotate and the other to reset the values. I was thinking it would be easier to just use a list box and have the three options and use a switch statement to execute each option. The only thing I'm unsure of is how to have System.Diagnostic.Process.Start execute the rotate.exe and pass the parameters I'm setting for each rotate option. Any ideas?... I couldn't find anything that covered this exactly, but if you find anything of reference that may be helpful, please let me know! Thanks!
Process.Start can start with arguments.
Sample
Process.Start("rotate.exe", "90");
http://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx
Or use ProcessStartInfo.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "rotate.exe";
startInfo.Arguments = "90";
Process.Start(startInfo);
See: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.processstartinfo
You should take a look at what options Process class offers. Take a look at [a this] (http://www.dotnetperls.com/process).
static void OpenMicrosoftWord(string f)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = f;
Process.Start(startInfo);
}

How to know when Android device is plugged into USB port? [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 9 years ago.
Improve this question
How can I tell if an Android device is plugged in to a USB port? I've seen many code samples that detect when any USB device is plugged in, but I can't use that. I need to be able to detect an Android device.
Question isn't clear to me, do you have to detect every Android device, or you want to detect just your private device?
Though, this may not be what are you searching for, but the easiest approach you could try would be to invoke command adb devices on USB device connection event and parse the output. I can't give much code right now, because of limited time I have, but take a look at
WinUSB C#
You can start process and parse its output.
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = #"C:\Windows\System32\cmd.exe";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
System.Diagnostics.Process p = new Process();
p.StartInfo = psi;
p.OutputDataReceived += p_DataReceived;
p.EnableRaisingEvents = true;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.StandardInput.WriteLine("adb devices");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
static void p_DataReceived(object sender, DataReceivedEventArgs e)
{
// Manipulate received data here
Console.WriteLine(e.Data);
// if no devices, then there will be only "List of devices attached: "
}
Once again, it's only a fast example and there should be a better solution for it. You could check Device ID upon connection, but it makes another problem - how to know that this ID is ID of an Android device?
EDIT: You also have to add ADB executable to your PATH environmental variable.
EDIT2: If you don't want to rely on PATH, you could provide ADB with your application and run it from app's directory, getting it with Application.StartupPath
EDIT3: Another drawback is that you have to enable USB Debugging in your Android device before ADB can detect it.

Get data from EXE [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have requirement, where I need to call application or EXE, whatever output of that application will be, I need to get that data in my application/code.
Just say I have an application or EXE which gets detail of computer like PC name, IP address, MAC address etc etc. My code/application shoud automaticaly trigger this EXE and get all PC data in my applicaion/code.
But above all is it realy possile??
But above all is it realy possile??
Yes.
Yes. You can start the process, redirect its output and parse it.
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// parse the line
}
Try having a look at Process.Start and Process.StartInfo

Categories