I am struggling in a issue related to windows services.
In my project , I want to change the sql service 'LOG ON AS' Local System using c# code.
You can do it in C# this way:
var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments= "/c sc.exe config \"mssql$sqlexpress\" obj= \"localsystem\" password= \"\"";
process.Start();
Note:
Pay attention to space after obj= and password=
isntead of mssql$sqlexpress, use your service name.
you may need run as administrator.
If you open the command prompt and run this command, your service account will change to local system account:sc.exe config "mssql$sqlexpress" obj= "localsystem" password= ""
Related
I was not able to run cmd process from back end of application with Process class hosted in IIS 10.0.
Note: I have found to start new process from IIS application pool need to add external options or privileges
Add administrator user in application pool identity
Allow to interact with desktop from IIS Admin service
After IIS 7.0 version there is no IIS Admin service. There is only World Wide Web Publishing service
I have tried
Add Administrator user for application pool in IIS
Switch on an option Allow to interact with desktop from World Wide Web Publishing service
My IIS version is 10.0
Process process = new Process();
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "cmd.exe";
processInfo.Arguments = $"/c python {NNModulePath}";
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.UseShellExecute = true;
process.StartInfo = processInfo;
process.Start();
Actual result I was not able to run cmd process from back end side
Expected result successfully run cmd process from back end side
I have c# web application project in which i want to install user uploaded .ttf font files into system.
I am using FontReg for the same. We can execute by using command line parameters as D:\TFS\Dev\Sprint_18_III\UI\Web\FontFiles>FontReg /copy so it will install all .ttf files present in directory
same this i am trying to achieve in c# code
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Verb = "runas";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "D:\\TFS\\Dev\\Sprint_18_III\\UI\\Web\\FontFiles\\FontReg.exe";
startInfo.Arguments = "/copy";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
Process gets completed but font doesn't install. What i am missing here ?
I dont want to give admin rights to entire web application running. It should be specific to one method only.
I believe that you are running the program with a under-privileged user... the IIS user, by default is "Network Authority", and this account cannot perform changes in the windows directory... try running this with a admin account...
I am trying to run a jar in C# by running this code:
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(pathForjre+"java.exe", "-jar "+jar+" "+argsforjar);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = processInfo;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = false;
string out = "";
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
out += proc.StandardOutput.ReadLine();
proc.Close();
return out;
When the jar runs I get an access denied exception with the path the jar is trying to write the log file to. I can manually run the jar from the command line and the log file writes no problem. How do I give the process permission to do things like write a file?
ASP.NET apps by default don't impersonate the identity of the user that is hitting the site. Typically then run as the local Network Service account unless you specifically have the process run under a different identity (you can determine the identity it's using by looking at Environment.UserName).
You can either set up the site (or use impersonation) to run under an identity that has permission to write to that location, or give write permision to the account that it's currently using.
Setting the working directory of the process resolved the issue.
proc.StartInfo.WorkingDirectory = path;
I wrote this code to open my application - the name of the executable is C# code analyser.exe. When I start it under Windows 7 (I don't know how this behaves under different versions of Windows), it displays the following message.
Do you want to allow to following program to make changes to this computer?
So I want Windows to not display it to me! What must I do to prevent this message from displaing?
System.Diagnostics.Process Process = new System.Diagnostics.Process();
Process.StartInfo.FileName = (System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "C# code analyser.exe"));
Process.StartInfo.WorkingDirectory = (System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "C# code analyser.exe"));
Process.Start();
Use this instead of your code
System.Diagnostics.Process oProcess = new System.Diagnostics.Process();
oProcess.StartInfo.FileName = "HelloWorld.exe";
oProcess.Start();
or you can pass administrator username & password this way
Process.Start(path + "HelloWorld.exe", uname, password, domain);
This analyzer project, most probably, have a manifest that request administration mode to run. This means that it will keep raising UAC if the starter process(your app) is not elevated.
You can try run you application as an administrator (right click-run as admin) and then the analyzer will inherit the elevation and it will not raise the UAC message.
I have the below code in ASP.NET C# and it work fine in local system + production machine when i tested in debug mode. but it doesn't work when i uploading to IIS.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Server.MapPath(filePath);
startInfo.Verb = "print";
startInfo.Arguments = "Printer Name";
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit(5000);
if (proc.HasExited == false)
{
proc.Kill();
}
Things i tried.
Control panel > Admin Services > Services > IIS Admin Service > Log on Tab > check to interact with desktop. Reset IIS Admin and IIS.
Printer Properties > Security > Grand ASPNET, NETWORK SERVICE, EVERYONE to full access.
Tried to set another printer as Default Printer. Reinstall / Add Printer.
I tried all the above with no success. finally i tried below in my machine.config.
WINNT>Microsoft.NET>Framework>v2.52something>Config> machine.config
I replaced this
processModel autoConfig="true"
with this
processModel userName="SYSTEM" password="AutoGenerate"
and i am getting this message
"Before you can perform print-related tasks you need to install a
printer"
i am using acrobat 7 and i can print the test page from printer itself and from acrobat software.
you can use Verb if acrobat is installed on you machine. and pass printer name as arguments
var fileName = #"c:\pdf\file.pdf";
var startInfo = new ProcessStartInfo(fileName);
string verbToUse = "PrintTo";
startInfo.Verb = verbToUse;
startInfo.Arguments = "PrinterName";
Process p = Process.Start(startInfo);
The issue may be that IIS runs under a different user that has less permissions than a typical User. See System.Diagnostics.Process.Start not work from an IIS
I fixed it on my server by changing the ProcessModel Identity to a user that has permissions. Probably a workaround and bad practice, but it worked. {Application Pool} -> Advanced Settings -> Identity -> Custom Account (Also toggle Load User Profile to true)