I NEED to run Task Manager with the very specific code that I have, but it is appearing with an access denied error.
I have attempted to run in administrator mode before.
FileStream fs = new FileStream(System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
The expected result I want is that Task Manager opens using the code above, without administrator rights! (Is there anyway around this?)
Use this:
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo(); //a processstartinfo object
startInfo.CreateNoWindow = false; //just hides the window if set to true
startInfo.UseShellExecute = true; //use shell (current programs privillage)
startInfo.FileName = System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"); //The file path and file name
startInfo.Arguments = ""; //Add your arguments here
Process.Start(startInfo);
Resources:
ProcessStartInfo - MSDN
This is a start process function I have
using System.Diagnostics;
private static void StartProcess(string exeName, string parameter)
{
using (Process process = new Process())
{
process.StartInfo.FileName = exeName;
process.StartInfo.Arguments = parameter;
process.EnableRaisingEvents = true;
process.Start();
}
}
Then call it like
StartProcess("exename.exe", fileParameter);
Process Class
Related
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
I Need to open a specific page of a pdf file.
I tried:
private void Button_Click_20(object sender, RoutedEventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=5\"";
startInfo.FileName = #"J:temp.pdf";
process.Start();
}
but it still opens the first page. Still unsolved.
if i Change to this
private void Button_Click_20(object sender, RoutedEventArgs e)
{
{
Process process = new Process();
process.StartInfo.Arguments = #"/A \"page=5\" \"J:\\temp.pdf"";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
}
}
i get seven Errors (Semicolon, page no context...)
It's unclear on how the arguments need to look like. Assuming it's J:\temp.pdf /A page=5
this should work:
Process process = new Process();
process.StartInfo.Arguments = #"/A page=5";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
However I'm not sure if you can pass arguments to a file name like this, I'd assume you need to call your PDF viewer's executable and pass both the file name and the page argument like in the question already linked in the comments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
startInfo.Arguments = "/A \"page=5\" \"E:\\Users\\You\\temp.pdf\"";
Process.Start(startInfo);
This works on my machine (replace paths as needed of course).
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
I am trying to execute a command by Process in c#, but problem is that that command ask a question (y/n) and process hang there. would you mind recommend me a solution?
public static OutputEventArgs execSync(string exe, string arguments)
{
OutputEventArgs oea = new OutputEventArgs();
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = exe;
startInfo.Arguments = arguments;
myProcess.StartInfo = startInfo;
myProcess.Start();
oea.Data = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
oea.exitCode = myProcess.ExitCode;
}
}catch(Exception e)
{
oea.Data = e.Message;
oea.ExceptionHappened();
}
return oea;
}
my command output is something like below:
C:\Users\abc>pcli Label -prI:\PVCS\DEVELOPMENT\
-idabcd:abcpass -v'test3' -f -z '/Project1/APPLICATION/ajax_fetchGetCustNew.php' Unknown os = Windows
NT (unknown) Serena PVCS Version Manager (PCLI) v8.4.0.0 (Build 668)
for Windows NT/80x86 Copyright 1985-2010 Serena Software. All rights
reserved. Version "test3" is already defined in archive
"I:\PVCS\DEVELOPMENT\archives\Project1\Application\ajax_fetchGetCustNew.php-arc".
Overwrite? (y/n)
using(var myProcess = new Process()) {
...
myProcess.Start();
myProcess.StandardInput.WriteLine("y"); // Write 'y' to the processes' console input
...
}
Note: This approach is not very reusable.
Using a command line option like /no-confirm (as John Wu suggested in the questions comments) is preferable, if it exists.
I'm using WinAppDriver in order to run some test cases on Excel. I'm trying to start the server through the code so that I don't have to manually do it in command line. I have the following code-
public static void StartWinAppServer(int port) {
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\Program Files (x86)\Windows Application Driver\";
startInfo.Arguments = $"WinAppDriver {port}";
process.StartInfo = startInfo;
process.Start();
}
Which is called like this-
public static WindowsDriver<WindowsElement> GetWindowsAppDriver (AppName appName) {
string appID = string.Empty;
StartWinAppServer(4723);
switch(appName) {
case AppName.Excel:
appID = #"C:\Program Files\Microsoft Office\root\Office16\Excel.exe";
break;
}
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", appID);
return new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
}
This code opens up the CMD but isn't running it. Am I missing something here? I thought the arguments property would've done the trick.
Try adding the /K or /C flag to startInfo.Arguments. This tells cmd.exe to run the following command and then close (in the case of /C) or return to the cmd prompt (in the case of /K)
startInfo.Arguments = $"/C WinAppDriver {port}";
https://ss64.com/nt/cmd.html