Launching process to open local html file results in Access Denied - c#

I am trying to open a local HTML file in the default web browser using C# and UWP.
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(#"C:\Users\Julie\source\repos\QuoteTool\QuoteTool\Assets\quote-pdf.html");
info.UseShellExecute = false;
System.Diagnostics.Process.Start(info);
I tried running the documention on https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo?view=netframework-4.8 , with the code resulting in 'Access Denied'.
Have also tried running Visual Studio as Administrator (same result)
Any guidance would be very helpful!!! I'm tearing my hair out!

Base on this code System.Diagnostics.Process.Start(string, string) first parameter means what you want and second means where you want so probably it can help you to solve your problem.Please see this document Process.Start Method
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("WINDIR") + #"\explorer.exe", pathToFolder);

Related

Using LibreOffice(soffice.exe) as Process.Start() from Code behind not working on IIS server

I am using LibreOffice as command line for conversion of docx to pdf. I am using below code snippet.
using (Process pdfprocess = new Process())
{
pdfprocess.StartInfo.UseShellExecute = true;
pdfprocess.StartInfo.LoadUserProfile = true;
pdfprocess.StartInfo.FileName = "soffice.exe";
pdfprocess.StartInfo.Arguments = "-norestore -nofirststartwizard -headless -convert-to pdf C:\\test.docx";
pdfprocess.StartInfo.WorkingDirectory = #"C:\Program Files\LibreOffice\program\";
pdfprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdfprocess.Start();
if (!pdfprocess.WaitForExit(1000 * 60 * 1)) {
pdfprocess.Kill();
}
pdfprocess.Close();
}
Everything works fine under IISExpress or Console application. When I try to run under IIS server, it doesn't work.
I am running under DefaultAppPool and I have given permission to DefaultAppPool to access LibreOffice directory but I am not able to get result.
I don't want to change Identity to LocalSystem as security concerns.
How can I able to run soffice.exe using Process.Start() under default ApplicationPoolIndentity?
I had the same problem and I just found solution that worked for me. When I was executing conversion under CMD console everything was working fine. But soffice.exe executed under iis app didn't work.
Although, application pool has it's own user profile directory it looks like libreoffice cannot create its files there. What I did is I created temp folder under iis www directory and gave it apppool permisions. Then I passed this location with other parameters like that: "-env:UserInstallation=file:///C:/www/temp/libreoffice"
Had same issue. The problem was that process started, but didn't exit and give no results, any StandardError output. It was definitely a problem of permissions. So I've started procmon and it showed me, it needs access to this path:
C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\LibreOffice
Looks like similar problem here:
Why does systemprofile need Desktop folder to open excel file
So not sure what to do next from security point of view, but solution is to give access to that folder for ApplicationPoolUser.
UPDATED:
Looks like it works not for all documents, and even after I eliminated all ACCESS DENIED issues from Procmon for other docx file, "soffice" still stuck, and I cannot kill process from Task Manager, it gives "Access Denied" dialog. Looks like it depends on some fonts, it opens something, and because it hold it, process cannot be closed, and no more files proceed.

C# Process.start opening application but not the file

I have an IE plugin which adds buttons in a page where there are pdf links and opens them in a specific application when clicked.
Lets say I need to open a xyz.pdf file in abc.exe application. abc is not the default application for file type .pdf.
In one machine the below works
Process p = Process.Start("pathtoabc.exe", "pathtoxyz.pdf");
In another machine it only works if I make abc.exe as the default app and then use the below
Process p = Process.Start("pathtoxyz.pdf");
Can you give me any pointers please? I also tried using ProcessStartInfo with no change
Updates:
I tried using the default Acrobat reader with an argument
Argument for processstartinfo looks like this "C:\PDF Files\Professional-Letters-Guide.pdf"
FileName = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
Result - Acrobat reader opens but with an error message "File not found". The is available in the path though.
Solved:
It was a space in the Foldername.. solved it by enclosing the filename with quotes "\""
Thank you all for the suggestions.. they helped me think it through.
Have you tried qualifying the Process.StartInfo.Arguments value with quotes and the full path to the file? What about the WorkingDirectory property? Also, the previous assertion regarding confirmation of the application being called supporting command line parameters is absolutely valid. You can be fooled into thinking that it does due to operating system file extension associations specific to a machine.
The second parameter of the Process.Start is passed to the application you are trying to start and it wont open the file using this application "pathtoabc.exe" unless the application "pathtoabc.exe" accepts the file name as a startup argument.
So you need to check if the application you are trying to use supports this kind of argument.

How to fix DirectoryNotFoundException while using Process in Web App

I'm using Pdf2Text in an ASP.NET web app. The web interface allows PDF files to be uploaded and converted to text. To convert to text, I use the C# function below, which relies on running the Pdf2Text program via the Process library.
void ExtractOCR(string input, string output)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.FileName = Server.MapPath("ocr/Pdf2Text.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = input + " " + output;
Process exeProcess;
using (exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
I've double-checked that the input and output paths are all valid. However, when I run the web app, I get the following error.
I've tried the Just-In-Time debugger but it won't even run for some reason. The Pdf2Text is a precompiled file, I don't have it's source code. I believe this is the file's download site, but not 100% sure. I've checked online to find solutions to similar errors but none has worked.
Thank you #GraDea for suggesting to look into the pool's permissions into the web app directory, this was the solution.
The web app was located at a custom location (not the default ASP server directory of inetpub\wwwroot). After the suggestion, I tried adding the pool user to the web app's custom location, but that didn't seem to work. Next, I moved the web app to the inetpub\wwwroot location and added the pool user to the folder, restarted the site via IIS and everything is now back to normal.
For future reference to anyone, easiest fix to a similar problem is to make sure your web app is in the default IIS server directory, and that your site pool's username is added to the application folder's security permissions. I've not tried the fix for a custom location because it's not so important for me, but I'm guessing it will most likely also involve adding the necessary IIS default pool users (e.g. IIS_IUSRS) and the site pool's user.

Process.Start is not working in IIS. Any options to open windows folder path.eg: C:\Newfolder

I have a requirement to open a windows explorer Path like this "C:\New Folder\" or "http:\gmail.com" using C# code. Actually, once the user types the link\path in the text box and save it, a link should appear and on clicking the link, the required folder path or link should open. My requirement is satisfied with Process.Start(). But, Process.Start is not working in IIS. Can any one suggest any other options. Please.
The code am using is below.
string myPath = #"c:\backoffice";
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
You are misunderstanding the way the web works. If you call Process.Start on the web server, it runs that command on the web server. There is no way that the Process.Start is magically mapped to some action on the client.
Also, you can't just open a specific folder on the client machine from a web site. The security protocols implemented in the browser will prevent that.

Calling unix shell script remotely from C#

In my current project, i need to call a Unix shell script from the C# application. I also need to get the response back whether the script has been execute successfully or any error has occurred.
The C# program is running on a Windows machine. I need to connect to a Unix machine and execute the script.
Can anyone let me know how this can be done using C#?
Will this solve your problem?
sharpSsh - A Secure Shell (SSH) library for .NET
Update
Refer to the developer's site for SharpSSH for more information on how to use the tool.
Update 2
change link of developer site to archived link.
A straight forward way of preforming this using System.Diagnostics.Process
// Start the child process.
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = 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 error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
Even i had the same problem, i have googled for solution for around 1 month.
Finally, i have decided to use plink.exe (command line version of putty.exe) to connect to unix box and execute a script there.
You have to use plink through c# process, i have tried it and this works amazingly.
But rite now the problem i am facing is when i am running a script from c# process i am unable to pass arguments to that script. Probably it would be rite to say that i do not know how to do that.
Regards
-Aakash

Categories

Resources