C# - Opening text file with notepad minimized? - c#

It doesn't look like the Minimized style has an effect:
string logFilePath = #"c:\mylog.log";
ProcessStartInfo startInfo = new ProcessStartInfo(logFilePath) {WindowStyle = ProcessWindowStyle.Minimized};
Process.Start(startInfo);
Thanks.

The Community Comments section of the MSDN page for the WindowStyle Property says:
To use Hidden, you'll need UseShellExecute = true
To use Hidden, you'll need UseShellExecute = true among other things. These requirements should be noted in the documentation, but aren't.
Have you tried that? Maybe it applies to Minimized as well.

Related

Working a process in background

I am using C# to run another .exe applicaiton.
however I want to run the .exe in background
I tried Start info hide bu.t it didn't help
How can I do that?
currently this is the code I am using
_p = new System.Diagnostics.Process();
_startInfro = new System.Diagnostics.ProcessStartInfo();
_startInfro.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
_startInfro.FileName = tmp[0];
_startInfro.Arguments = deleteRangeCommand;
_p.StartInfo = _startInfro;
_p.Start();
Since I you don't provide information about which .exe you want to start I can't be sure about this answer. But the CreateNoWindow property looks like what you need.
You must also set the UseShellExecute property to false as is pointed out in the documentation. Otherwise the value of CreateNoWindow is ignored.

Process.Start starts a shell then quickly exits

I am writing a C# application that uses WinForms.
I want to launch my own shell as an Admin then use Stream Writer to run some commands.
I am not doing any thing malicious. It's to fix the connection issues with another internal application (for work). The other team isn't willing to fix their program so they provided us with a command line fix. However I have to run this on many PC's so I am learning C# and trying to build something that I would use.
As soon as myProcess.Start executes, a black box appears then quickly disappears before going to the next line.
public void ProcessStartAsAdmin(string command)
{
string cname = Environment.UserDomainName + "\\" + Environment.UserName;
Process myProcess = new Process();
myProcess.StartInfo.FileName = #"C:\Windows\System32\CMD.exe";
myProcess.StartInfo.Verb = #"runas";
//myProcess.StartInfo.Arguments = command;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
String inputText;
string resetConnection = "C:\\APP\\application.exe /reset /server:example.com /uid:" + cname + " /pwd:" + textBoxPassword.Text.Trim();
myStreamWriter.WriteLine(resetConnection);
myStreamWriter.Close();
myProcess.WaitForExit();
myProcess.Close();
}
If I am doing anything wrong, please let me know. If I can use a better technique, I'm open for suggestions.
Thanks.
I don't know whether this is the only issue, but there's no space between these two arguments:
/uid:" + cname + "/pwd:"
it should be:
/uid:" + cname + " /pwd:"
string resetConnection = "C:\\APP\application.exe
\a is illegal. You should write \\ (it seems you know it, and missed).
Edit:
The screen that your see is empty, because of you chose to redirect output.
What is probably happening is that the line has some error (or it's running time is very small), and you not see that.
Cancel temporary the output redirection, and the process closing, and see what happened.
Let's take a look at how you initialize the start info:
myProcess.StartInfo.Verb = #"runas";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardOutput = true;
You are redirecting the standard input, output and error streams. The documentation for this says:
You must set UseShellExecute to false if you want to set RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception.
And likewise for the other two standard streams. So, redirection implies that you must set UseShellExecute to false.
On the other hand, you wish the new process to be elevated. Which means that you want to use the runas verb.
The documentation does not say so, but the Verb property only has impact when UseShellExecute is set to true. So, requesting elevation implies that you must set UseShellExecute to true. There is no other way to request elevation for a new process. You have to pass through the ShellExecuteEx (or its relatives) with the runas verb and that forces you to set UseShellExecute to true.
So there are two conflicting requirements here. You've got no option at all over elevation I presume. You must elevate for this task. Which means you'll have to give up on redirection. I suppose the only other option would be for you to run the calling process elevated. That would mean that any child processes would also be elevated and there would be no need for runas and so you could set UseShellExecute to false.
However, I see no reason for redirection. You are only doing that, I think, so that you can send commands to the cmd process. Instead you can pass the commands as arguments, or place them in an external .bat or .cmd file. So if I were you I would stop redirecting, and set UseShellExecute to true, and take it from there.

Process.StandardOutput.ReadToEnd() and extraneous newline chars

I am trying to understand why, when I call the above function, I am getting hex 0D0A every 80th column on the output I am reading.
I have a powershell script, for testing that has two lines in it for brevity's sake:
$xmlSpew = "<IISAppPoolConfig><DefaultWebSite><ApplicationPoolName>DefaultAppPool</ApplicationPoolName></DefaultWebSite><AuthN>Basic</AuthN></IISAppPoolConfig>"
Write-Output $xmlSpew
I am calling the script using the Process object with ProcessStartInfo as follows:
var psi = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(FileToRun),
FileName = FileToRun,
Arguments = Arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
var process = new Process
{
StartInfo = psi,
EnableRaisingEvents = true,
};
FileToRun value is:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments value is:
-File "C:\Program Files\MyTest\MyProgInputs\read.d\IISAppPoolConfig.ps1"
The script runs fine and I get back exit code 0, but I have this mysterious (to me) 0D0A newline every 80th char in standard out that I capture using:
var Stdout = new List<string>;
...
Stdout.Add(process.StandardOutput.ReadToEnd());
This is wreaking havoc on my XML efforts once I have standard out stored in a string var. I expected to get exactly what I write to the stdout in the ps1 script, not the extra newline.
What am I missing? I've looked for others with this issue, but I have not found an answer. Hopefully it is not me being search-challenged.
Follow this P/Invoke method and set dwXCountChars to a very large value. Don't forget to include STARTF_USECOUNTCHARS in the flags as well.
Final and tested resolution for now (because I need to ship something), is to have output from powershell come from the Write-Host cmdlet instead of Write-Output. The process for obtaining stdout remained the same as in my original post. Hope this helps others. Thanks for all the inputs.

batch file execution in c#

I am running a Java batch file from C#. If I run it by double clicking it executes successfully, but if I run it from C# code it gives exception in thread
"exception in "main" thread
java.lang.noclassdeffoundError"..
what can be the reason and how can it be solved? I am using the code:
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = "batch-file path";
si.UseShellExecute = true;
Process.Start(si);
You are most likely missing some of the parameters that would be included in your systems environment variables.
Try setting working directory like this
process.StartInfo.WorkingDirectory = "C:\";
Also, try few other options as mentioned here,
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/20992653-dabf-4b31-85f7-e7bfbfb4557c
Try adding the following code as the first line to your batch file.
#cd /d %~dp0
Do not use batch_process_path + "\" + instead use Path.Combine() to make sure the path is correctly fitted with slashes.
Also read this "When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable"
So set it to false.

Open a file with Notepad in C#

How I open a file in c#? I don't mean reading it by textreader and readline(). I mean open it as an independent file in notepad.
You need System.Diagnostics.Process.Start().
The simplest example:
Process.Start("notepad.exe", fileName);
More Generic Approach:
Process.Start(fileName);
The second approach is probably a better practice as this will cause the windows Shell to open up your file with it's associated editor. Additionally, if the file specified does not have an association, it'll use the Open With... dialog from windows.
Note to those in the comments, thankyou for your input. My quick n' dirty answer was slightly off, i've updated the answer to reflect the correct way.
You are not providing a lot of information,
but assuming you want to open just any file on your computer
with the application that is specified for the default handler for that filetype,
you can use something like this:
var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = fileToOpen
};
process.Start();
process.WaitForExit();
The UseShellExecute parameter tells Windows to use the default program for the type of file you are opening.
The WaitForExit will cause your application to wait until the application you luanched has been closed.
this will open the file with the default windows program (notepad if you haven't changed it);
Process.Start(#"c:\myfile.txt")
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
You can use Process.Start, calling notepad.exe with the file as a parameter.
Process.Start(#"notepad.exe", pathToFile);
Use System.Diagnostics.Process to launch an instance of Notepad.exe.

Categories

Resources