I wrote a program to get rid of unexpected errors like NULLs or zeros in .xml file after crash but the "restart" part of code isn't working. It's all good when I run the code in Visual Studio Code but when I use .exe file from dotnet publish function the program just crashes.
I've already tried setting UAC at level 0, UseShellExecute true/false, System.Diagnostics.Process.Start();, running as administrator.
static string exeAdress = #"C:\Program Files (x86)\NaturalPoint\SmartNav\SmartNAV.exe";
// Process.Start(exeAdress); // this isn't working either
Process p = new Process();
p.StartInfo.FileName = exeAdress;
p.StartInfo.UserName = "User";
p.StartInfo.Domain = "Domain";
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;
Actual output is throwing exception but I expect to run the exe without errors:
Unhandled Exception: System.ComponentModel.Win32Exception: The requested operation requires elevation
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
You need to run your main application as administrator (with elevated permission).
If you can run your application with elevated user then you do not need to supply
p.StartInfo.UserName = "User";
p.StartInfo.Domain = "Domain";
parameters.
Related
I am using .Net 4.6.1 for a Winform application.
I neet to execute some command to configure user settings through powershell.
The powershell reference that I used is under C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0, so (I guess) its version is 3.0.
I use following to execute powershell command:
System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create();
shell.AddScript("Get-LocalUser -Verbose");
then check
shell.Streams.Error[0].Exception;
and see following error:
The term 'Get-LocalUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I also tried executing the command through a powershell file like:
var psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Verb = "runas";
psi.FileName = #"powershell.exe";
psi.Arguments = $" -File \"<path-to-file>\\MyScript.ps1\"";
try
{
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
string f = proc.StandardError.ReadToEnd();
proc.WaitForExit();
exitCode = proc.ExitCode;
}
But I got the same error.
How can I call a powershell script or powershell within c#?
EDIT: it turns out to be 32-64 bit issue. When I switched to x64, it worked just fine.
You should ensure you compile your C# code to target x64 (i.e. 64-bit) as most PowerShell modules will only work in 64-bit mode. If your C# code runs in 32-bit mode, it will invoke a 32-bit PowerShell environment, which will cause problems.
I had the similar experience and succeeded only after changing ExecutionPolicy. The error was equal, but all the details seemed fine.
PS 7.1.3, Microsoft.Powershell.SDK.
Use this:
PowerShell.Create().AddCommand("Set-ExecutionPolicy")
.AddParameter("-ExecutionPolicy", "Bypass")
.Invoke();
Run this code once before doing anything with the PowerShell.
The context here is that we have a C# service that normally runs scheduled jobs, but an executable was created that allows jobs to be triggered manually via command line. We put that executable on a separate IIS server, and did NOT install it as a service. The code inside the app to determine how it's being run is simply:
if (Environment.UserInteractive)
{
//parse the parameters and run the specified job
}
else
{
//start the service jobs
}
I made an API as a wrapper to call that executable, which uses the following code to run the executable with arguments as a user of the machine.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = (absolute path of the folder that contains the exe),
FileName = (absolute path to the exe),
Arguments = (args),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Domain = (domain),
UserName = (username),
Password = (password),
Verb = "runas"
}
};
proc.Start();
proc.WaitForExit();
The API and the exe live in the same base folder. The API runs in IIS under an app pool user that is the same user it is running the process with. This user has Full Access permissions to the folder and executable, as well as the app pool user. We also added the user to the Administrator's group on that machine.
Running the exe via command line locally on that machine works fine. Only when calling from this application do we get the following error:
System.ComponentModel.Win32Exception (5): Access is denied
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
I've confirmed that we are targeting the right file, that my SessionId is not 0 (I would get an error saying the service was not installed whenever I didn't start the process as a specified user), and that the app pool user and windows user have permissions to execute the file. UAC is off, and the API and exe are not on the C:/ drive. After hours of googling and trying different things, I'm out of ideas. Any help would be very appreciated.
I want the C# code to open a program using a shortcut. The program requires admin privileges.
Whilst ensuring Visual Studio is running as administrator,
I have tried:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "FN.lnk";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
This fails with the exception: 'The operation was canceled by the user'.
Replacing "FN.lnk" with "cmd.exe" runs a command prompt window with admin privileges...
I have also tried:
System.Diagnostics.Process.Start("CMD.exe", "/K FN.lnk");
which launches an elevated command prompt window, but still says "Access is denied"
If I run cmd as administrator and manually navigate to the folder to run FN.lnk, it works perfectly... which makes this problem even more confusing.
This is my code block:
Process myProcess = new Process();
myProcess.StartInfo.FileName = fullpath;
myProcess.StartInfo.Arguments = " " + strNewFilePath;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
I am running an exe file using the System.diagnostics.process and i am keep getting this error:
System.ComponentModel.Win32Exception (0x80004005): The application has
failed to start because its side-by-side configuration is incorrect
The machine i am running this on is x64.
Googled it for a couple of hours, not much info on this. Any ideas?
Which application are you trying to run? Is it a 32-bit or 64-bit app?
You can check the event viewer Application log to figure out more information about this error.
If it due to some VC runtime then you can install the appropriate x86/x64 VCredist package (2005/2008/2010/2013) and see if it works for the application that you are trying to launch.
i'm starting a child application from my main process with the following code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = DestinationNameEXE;
startInfo.WorkingDirectory = Toolbox.AssemblyDirectory;
startInfo.UseShellExecute = true;
startInfo.Arguments =
binParameter + #" """ + _BINPath + #" """
+ tmpParameter + #" """ + binFolder_ForExtration + #" """;
Process.Start(startInfo);
before i do this i check if the process has write rights in the binFolder_ForExtration and _BInPath with following code (source) and a simple write file check:
try
{
using (File.Create(tmpfile)) { }
File.Delete(tmpfile);
createFilesAllow = true;
}
catch
{
createFilesAllow = false;
}
in the main process i can write & create files. also the function HasWritePermissionOnDir returns true.
in the new process i'm doing the same test, but in this case i got from HasWritePermissionOnDir true, but if i try to create a file, i got an exception. the new process has the rights to change existing files.
how can this be? what i'm doing wrong?
how can the new process have less rights then the first process?
i don't change the user context.
i' have also tested to start the process with ProcessAsUser.Launch (source) but without any change. the new process dond't have the right to create new files in the given folder.
if i start the process with elevated rights:
startInfo.Verb = "runas";
everything works finde. the new process can write, create and change files in the given folder.
thx for any help
Update:
i'm testing on a windows 7 x86
the main process is compiled with target x86 and .Net 4.5. its a dll called from a vb6 application
the user i'm testing with has admin rights (but it's not the user administrator). the main process is started with normal rights settings (not elevated rights)
the new process is compiled with target any CPU and .Net 4.5.
since i require access to Process.GetProcesses in the new process it should be compiled as any CPU to be used on x86 and x64 systems (as far as i know)
currently all folder tests and occured errors happend in the folder *c:\Program Files (x86)\appname\BIN*. in the code i'm using full path and test the correctness of the path with direcotry.exist() bevore any execution.