how to open CMD using iis application windows 10 using C# - c#

how I can open CMD using my IIS Application
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory ="c:\\pdftohtml\\";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "MyText";
Process proc = Process.Start(startInfo);
I believe this code is true but dose not work with IIS for some reason
Thanks in Advance

Opening a command prompt from an IIS application is not a good idea.
If you need to run a tool, maybe it's better to find the API used by the tool and reference it from a service called by the IIS app.
If you need to convert pdf to html, check this link

Related

C# start Child Process (in CMD window) as Administrator with custom environment variables

I have a program which wraps around some Windows SDK executables and opens them in a separate CMD window.
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C signtool.exe [args] & pause";
process.StartInfo.Verb = "runas";
process.Start();
Right now, I have the Windows SDK folder added to my system's Path environment variable. Is there a way to programmatically add the Windows SDK folder to the Path environment variable of the user OR run the process with the SDK folder added to the Path variable of that particular CMD window?
This is the folder I need added to each CMD window's Path variable:
C:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x86
This sub-process must run as administrator. It does not need to receive the output of the child process.
Use a ProcessStartInfo and its Environment property instance to set this up.
var startInfo = new ProcessStartInfo();
var defaultPath = startInfo.Environment["PATH"];
var newPath = "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.16299.0\\x86" + ";" + defaultPath;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c set > D:\\env.txt";
startInfo.Verb = "runas";
startInfo.Environment["PATH"] = newPath;
startInfo.UseShellExecute = false; // required to use Environment variables
Process.Start(startInfo);
There are a number of hurdles to overcome here.
As you've discovered, the Environment and EnvironmentVariables properties of ProcessStartInfo cannot be used when UseShellExecute is true (an exception is thrown). But Verb only works when UseShellExecute is false (the Verb is silently ignored). This comes down to the differences between CreateProcess (the core Win32 function) and ShellExecute/ShellExecuteEx (the shell function).
As another commenter suggested, you might try setting the environment in the parent process and then starting the child process. However, elevated processes don't inherit the environment from a non-elevated parent process.
I would be willing to bet that there is a way to do what you want using a correct series of Win32 calls to get an elevated token and then calling something like CreateProcessAsUser. I am also willing to bet it'll be a little error-prone and ugly in C# because of the necessary struct marshaling. So instead of trying to figure that out for you, I'll offer another suggestion:
Just programmatically write a batch script that sets the environment and invokes signtool.exe. You can then invoke that batch script using the runas verb as you're currently doing.

C# cmd prompt cannot see telnet.exe

I used C# with a console program to create a new cmd process, did not redirect stdin or stdout, so I could type into the command line from here.
(I was having trouble using telnet from there, so this step was just an investigation.)
Able to type into the window and receive output.
When I switched to c:Windows\system32, typing dir te*.exe shows nothing.
In another command prompt I created directly, I see the file (telnet.exe).
Any suggestions about what is wrong?
{
ProcessStartInfo startInfo = new ProcessStartInfo(#"cmd.exe");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.CreateNoWindow = false;
startInfo.Arguments = host;
using (Process p = new Process())
{
p.StartInfo = startInfo;
p.Start();
}
}
Since Windows 7, I believe, you have to install Telnet as a Windows Feature.
Here you have a guide to enable Telnet on Win 7, but it's applicable to Win 8.1 as well as Windows 10.
Just in case you can't read the site, the steps are:
Go to Control Panel -> Programs -> Turn Windows Features on or off -> Scroll down until you find the Telnet Client option
Based on the above article, looked at project build properties.
Platform target was set to x86.
Changing to "Any CPU" at least allows me to see the program!
BTW I have looked for the answer for several days before posting this, but in the margin in related - "C# New process created cannot access certain files" gave me the info - after I created this question
Thanks, heuristics!
This is a really devious one. When you are using windows explorer or opening a command prompt directly, you are starting a 64-bit process. When you are starting the "cmd.exe" with Process.Start(), you will get the same version as the process that's starting it. In your case, you are creating a 32-bit process, so you get the 32-bit version of the command prompt. If you change your project to create target x64, you will see the files!
Why is this so? Because, depending on whether you are accessing System32 through a 32-bit or 64-bit app, you will actually be accessing different System32 folders! For more on this, follow this link:
https://superuser.com/questions/330941/some-files-in-system32-not-accessible-outside-explorer-on-windows-7

Guidance for invoking .jar file through asp.net code

ProcessStartInfo psi = new ProcessStartInfo("java.exe", " -jar \"C:\\craFVUsubsreg-lite_APY.jar\"");
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
I want to invoke the utility tool, which is of java.jar file within the "c#" [mvc] application and it is work fine when i run the application through code. However after i published the application and then i try to invoke the same jar file it is not responding or i can say it is not working.
Above is the process code which i'm using to invoke the jar file. Even the user permission i have setted to full control mode.
Kindly guide me, where i am wrong, or is their any way to directly invoke the java.jar file without converting to java.exe.

Starting exe with Process.Start(), weird stuff going on

Im currently trying to automate the Microsoft Assessment and Planning Toolkit. But I'm running into a problem.
When I start the toolkit by clicking the icon on my desktop, the toolkit works fine. But, if I execute the exe by using Process.Start(), the toolkit bugs. (After a change of the selection in the left pane, the options are not shown. Next to this, when the toolkit is closed an error is shown)
Started from shortcut on my desktop:
Started with Process.Start():
The code for starting the exe:
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Program Files\\Microsoft Assessment and Planning Toolkit\\bin\\MapToolkit.exe";
Process p = Process.Start(start);
Anyone has any idea what this could be, or how I could solve this behaviour?
Try using WorkingDirectory:
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Program Files\\Microsoft Assessment and Planning Toolkit\\bin\\MapToolkit.exe";
start.WorkingDirectory = "C:\\Program Files\\Microsoft Assessment and Planning Toolkit\\bin";
Process p = Process.Start(start);

svn check out using C# program

I am writing a small utility to execute svn commands using c# program
Here is the key line in my program
System.Diagnostics.Process.Start("CMD.exe", #"svn checkout C:\TestBuildDevelopment\Code");
As I assume, the above code should be able to do svn check out and download all the code to the local path mentioned above. But what's happening is that, the command prompt opens up with the default path of the c# project and does nothing.
If I run this svn command in command line it works fine. But when I run using C# it just pops up the command prompt without executing the svn checkout operation. Any idea on what is going wrong?
You don t have to run CMD.exe. CMD.exe ist just a program that calls other assemblies.
You can call svn.exe directly with your argument checkout ... (but isnt there a url missing?)
Process.Start("svn.exe", #"checkout C:\TestBuildDevelopment\Code");
you may also try this:
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "svn.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "checkout ...";
proc.StartInfo = startInfo;
if(proc.Start()) {
proc.WaitForExit();
}
You need to add the parameter /c. Try:
Process.Start("CMD.exe", #"/c svn checkout C:\TestBuildDevelopment\Code");
The parameter /c means that the cmd should execute the command and exit.
As Jon notes, svnsharp would be a good choice here; but IMO the problem is shelling cmd.exe, rather than the exe you actually want to run:
Process.Start(#"path\to\svn.exe", #"checkout ""C:\TestBuildDevelopment\Code""");
although a ProcessStartInfo would make it easier to set the working path etc.

Categories

Resources