How to run 2 files c# without opening window - c#

I got 1 mapper (.exe) and 1 driver (.sys) and i want to do so that when i execute them i want to run mapper as admin with the driver and not create a window (keep it hidden). can anyone help. I have the following code down but nor does it run the mapper as admin with the spoof and it still also creates a window! help!
string map = "C:\\SCSpoofer\\mapper.exe";
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess = Process.Start(map, sys);
System.Threading.Thread.Sleep(150);
myProcess.Kill();```

// Create a Process to launch a command window (hidden) to create the item templates
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = Project.ServicesFolder;
startInfo.Arguments = "/C " + CreateServices;
process.StartInfo = startInfo;
process.Start();
This is from my open source project DataTier.Net github.com/DataJuggler/DataTier.Net

Related

Problem with running batch files from within my application

I've been trying to create a simple application to backup my Windows Server databases aswell as a whole server backup.
For this I want to use batch files which are being executed by my application.
I tried several approaches but for some reason it always fails so I'd be happy if you could help me out.
Batch file BACKUPSERVER:
wbadmin start backup -backupTarget:D: -include:C: -allCritical -quiet
I have to run the bat as administrator or it fails due to missing permissions.
C# code:
static Task<int> RunProcessAsync(string fileName)
{
............
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C \"D:\\SQLBACKUP\\BACKUPSERVER.bat\"";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
Debugging says 'wbadmin wasnt found'. 'runas' activated or not doesn't make any difference.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false;
// startInfo.Verb = "runas";
var process = new Process
{
StartInfo = { FileName = fileName },
EnableRaisingEvents = true
};
process.StartInfo = startInfo;
process.Exited += (sender, args) =>
{
tcs.SetResult(process.ExitCode);
process.Dispose();
};
process.Start();
Also doesn't work.
Any ideas?
EDIT:
I'm able to run commands like shutdown but wbadmin doesn't work whatsoever...
This is how I solved the problem:
Make sure ure compiling for 64bit if u intend to use your application on 64bit system, otherwise it will redirect to different subfolders and wont find 'wbadmin.exe'.
Run wbadmin with ProcessStart or run a batch but without direct cmd input, so use this with filename = batch file or wbadmin with startInfo.Arguments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false;
// startInfo.Verb = "runas";
var process = new Process
{
StartInfo = { FileName = fileName },
EnableRaisingEvents = true
};
process.StartInfo = startInfo;
process.Exited += (sender, args) =>
{
tcs.SetResult(process.ExitCode);
process.Dispose();
};
process.Start();
Make sure u request administrator rights

How to run some CMD commands one by one (As admin)

I have a few commands I want to run through the CMD (admin):
ipconfig/flushdns
ipconfig /registerdns
ipconfig /release
ipconfig /renew
netsh winsock reset
Anyone can tell me how?
Question of is sort answered here: Running cmd commands with Administrator rights
The code could be duplicated (as shown below):
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /flushdns";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo2 = new
System.Diagnostics.ProcessStartInfo();
startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/C ipconfig /renew";
startInfo2.Verb = "runas";
process2.StartInfo = startInfo2;
process2.Start();
If you are looking to use powershell, you can create a script which will include your commands something like this:
& "command 1";
& "command 2";
etc.

Open a specific page in a PDF file c#

I open a pdf file when my form is loaded with the following code:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
This works fine but now I want to open a specific page. For example page number 5 of the document test.pdf? Does any one have an idea? Tried some stuff but dind't work!
Thanks!
Try
process.StartInfo.Arguments = "/A \"page=n\" \"F:\\STAGE\\test.pdf"";
changing n to the page number you want
Checkout this : http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf
It explain what arguments Adobe Reader can receive.
And it has a Page argument.
Your code must be :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=N\"";
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
Where N is your page number.
call it like what was suggested here: Adobe Reader Command Line Reference
So it would be:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "EXE_PATH\\AcroRd32.exe";
startInfo.Arguments = "/A \"page=PAGE_NUM\" \"FILE_PATH\"";
Process.Start(startInfo);
you can try this code.
Process myProcess = new Process();
myProcess.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
myProcess.StartInfo.Arguments = "/A \"page={pagenum}\" \"c:\\Classic\\Manual\\DocumentationManual.pdf\"";
myProcess.Start();
please change the path of AcroRd32.exe as per your directory.
Thanks
Try this.
Note: you must have acrobat reader installed in your pc before you can use axAcroPDF .
int n = 5; //page number
string filePath = "F:\STAGE\test.pdf";
axAcroPDF1.LoadFile(filePath);
axAcroPDF1.setCurrentPage(n);

Run CMD command without displaying it?

I have created a Process to run command in CMD.
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
How can I run this command without displaying actual CMD window?
You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Source:
Property:MSDN
Enumartion: MSDN
And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c apktool d app.apk";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "apktool";
//join the arguments with a space, this allows you to set "app.apk" to a variable
psi.Arguments = String.Join(" ", "d", "app.apk");
//leave it to the application, not the OS to launch the file
psi.UseShellExecute = false;
//choose to not create a window
psi.CreateNoWindow = true;
//set the window's style to 'hidden'
psi.WindowStyle = ProcessWindowStyle.Hidden;
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
The main issues:
using cmd /c when not necessary
starting the app without setting the properties for hiding it
Try this :
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

C# New process window does not hide [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Hide Command Window in C# Application
in the console app, I want to run the cmd command, but a new window is being created, why ?
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\m_f";
startInfo.Arguments = "/c START _creator.bat";
process.StartInfo = startInfo;
process.Start();
edit
I had to change some code to:
startInfo.FileName = #"C:\m_f\_creator.bat";
startInfo.WorkingDirectory = #"C:\m_f\";
startInfo.Arguments = "some_args";
now it works
start spawns a new window, regardless of whether the parent shell has a window or not.
To run a batch file cmd /c foo.cmd suffices, or even foo.cmd.

Categories

Resources