Unlock section in applicationHost.config programmatically - c#

I have checked ServerManagaer class and it gives a lot of functionality to work with IIS, it also contains methods to update values in applicationHost.config file, but I can't fine any way to unlock sections there.
For example for that purpose appcmd.exe unlock config command is used. I need to do the same programmatically.

As already said you can run appcmd process. But just a hint that if you don't console to popup you can redirect the output.
Here is the code from MSDN
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
More details see HERE

To my knowledge you can't perform lock/unlock action using ServerManager but still you can execute appcmd.exe programatically to achieve the desired result:
System.Diagnostics.Process appCmdProc = new System.Diagnostics.Process();
appCmdProc.StartInfo.FileName = "Path-to-Directory\appcmd.exe";
appCmdProc.StartInfo.Arguments = "unlock config /section:sectionName";
appCmdProc.Start();

Related

how can I execute a shortcut(.lnk) using Process.Start()? [duplicate]

Is there a way to run an application via shortcut from a C# application?
I am attempting to run a .lnk from my C# application. The shortcut contains a significant number of arguments that I would prefer the application not have to remember.
Attempting to run a shortcut via Process.Start() causes an exception.
Win32Exception: The specified executable is not a valid Win32 application
This is the code I am using.
ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );
Could you post some code. Something like this should work:
Process proc = new Process();
proc.StartInfo.FileName = #"c:\myShortcut.lnk";
proc.Start();
Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.
if your file is EXE or another file type like ".exe" or ".mkv" or ".pdf" and you want run that with shortcut link your code must like this.
i want run "Translator.exe" program.
Process.Start(#"C:\Users\alireza\Desktop\Translator.exe.lnk");
If you're using UseShellExecute = false and trying to launch a batch file make sure to add .bat to the end of the filename. You don't need .bat if UseShellExecute = true though. This made me just waste an hour of work... hoping to save someone else.

Auto Log Off when opening specific folder (Homework)

The code below logs off the PC. But what I want is that when I click on a specific folder, for example: E:\Picture, Windows should log off.
I don't know how to put such condition in the code. Can anyone point me in the right direction?
Code for logging of:
using (Process proc = new Process())
{
proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
proc.StartInfo.Arguments = "-l";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
}
Since this is homework, here some starting points
Create a local hook on explorer.exe
Find out if you can use WinAPI to find process information on the explorer
...
execute shutdown.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644960(v=vs.85).aspx

How to run a command in terminal and capture the output

I am working on an application using MonoDevelop in Ubuntu. Through this application I need to run a terminal command and capture it's output. Is such thing possible? Any help/ideas will be appreciated!
What I mean is, if user clicks on a button, the command will be run and the output will be displayed in a text box or something. I don't want a terminal window pop-up, this action should be entirely done inside the application.
In C# Windows you would do this:
Process p = new Process(); // Redirect the output stream of the child process.
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "program.exe";
p.Start(); // Do not wait for the child process to exit before reading to the end of its redirected stream.
p.WaitForExit(); // Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

How to redirect standard output when spawning a process

I've built a webservice for an internal server that I want to send a command to and have it launch a process. So I want to send a command to it like this:
http://machine:999/execute?path=c:\scripts\dostuff.exe&args=-test
The endpoint would then do something like this:
public ActionResult(string path, string args)
{
var proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = path;
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.RedirectStandardOutput = true;
//Hook into all standard output here
//Block until process is returned - Async Controller action
return Content(output.ToString())
}
I want to be able to capture all the error messages and standard output generated by the executable. The executable could be anything like a console application or a powershell script. What's the best approach for doing something like this?
Use proc.StandardOutput.ReadToEnd() to read the redirected output stream to the end, and return that.
You may also want to set RedirectStandardError to True and do the same thing with proc.StandardError to get the error messages. You can spawn a background thread to synchronously read standard error alongside the reading of standard output in the main thread.

strange behaviour on another process via Process.Start(startInfo)

Our C# (V3.5) application needs to call another C++ executable which is from another company. we need to pass a raw data file name to it, it will process that raw data (about 7MB) file and generate 16 result files (about 124K for each).
The code to call that executable is this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = exePath;
startInfo.Arguments = rawDataFileName;
try
{
Process correctionProcess = Process.Start(startInfo);
correctionProcess.WaitForExit();
}
catch(nvalidOperationException ex)
{
....
}
catch(...)
...
It works fine. Now we have new raw data. After replace the old raw data with the new raw data file. That executable process never return to us. It will hang forever. If we kill our C# application, those result files will be generated in the target directoy. It looks like the executable does create those result files but has issue to write to the disk and return to us until the process is terminated.
It is NOT like this with the old raw data file.
When we run the executable with the new raw data directly (no from our C# app call), it works fine. This means this executable has no problem with the new raw data.
My question 1: what's the possible causes for this behaviour?
Now I change our code with startInfo.UseShellExecute = true; and add startInfo.WorkingDirectory= ..., and disabled
//startInfo.RedirectStandardError = true;
//startInfo.RedirectStandardOutput = true;
Then it works.
My question 2: why use Windows Shell solve this issue?
My question 3: why it works before without using Shell?
My question 4: when we should use Shell and When shouldn't?
thanks,
Several possibilities:
You are redirecting output and error but not reading it. The process will stall when its stdout or stderr buffer fills up to capacity.
The program might be displaying an error message and waiting for a keypress. You are not redirecting input nor check stderr, that keypress will never come.
Some programs, xcopy.exe is a very good example, require stdin to be redirected when you redirect stdout. Although the failure mode for xcopy.exe is an immediate exit without any diagnostic.
Seeing it fixed when you kill your C# program makes the first bullet the likeliest reason.
I know this, it is a very common problem. I has to do with the output, which must be handled asynchronously. You just can't WaitForExit when output exceeds certain amount of data.
You need to add
myStdErr= correctionProcess.StandardError.ReadToEnd();
Only once usually works, if you want to overkill this works ("P" being my Process)
while (!P.HasExited)
stdErr+= P.StandardError.ReadToEnd();
If you don't need the stdout/stderr, just turn the Redirect* properties to false.

Categories