C# Process.start opening application but not the file - c#

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.

Related

Launching process to open local html file results in Access Denied

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);

Process.Start for ftp:// prompts for app

I'm writing code that will start a download from our company's ftp (ftp://...) but when using Process.Start("ftp://..."); Windows will prompt me for an app to open it with (I'm using Windows 10). If I use Process.Start("http://www.google.com"); it doesn't prompt. How do I avoid this prompt and just navigate the user to the ftp URL?
Windows knows what to do with a URL that starts with http: open the default web browser and browse to that URL. However, it doesn't natively know what to do with a URL that starts with ftp.
When you're using Process.Start, think of it like running a command from the "run" line in Windows. You usually need to specify an executable to run, and any additional information -- i.e. arguments to the executable -- occur after the path or executable name.
In this case, I'd say you just want to start Internet Explorer and provide it your URL as an argument:
var psi = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(#"%ProgramFiles%\Internet Explorer\iexplore.exe"), url);
var proc = Process.Start(psi);
EDIT: to answer your question about using the default browser, see this SO answer about how to get the default browser's path:

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.

How can I include saving in a text file in executable file?

I have an application in Visual Studio C# which includes saving into a text file, how can I have a .exe sent to another computer and not have an exception in saving?
I need to send a .exe file by email (Yes it's possible) and this application includes saving the state of the game. How can I send this .exe file and let the user be able to save in his computer please?
The problem is that when I send my application's executable file on another computer, I'm getting an exception in saving. Because on the other computer I don't have the text file which I'm saving the game.
I am saving here :
StreamWriter myFile = File.CreateText(Directory.GetCurrentDirectory()+"//ConnectFour.txt");
in the obj/Debug/ of the project..
Thanks for your help :)
Sending an executable should work just fine.
Make sure the other computer has the appropriate Microsoft .NET Framework installed.
Latest framework installer: MSDN
Also, make sure the path inwhich you're saving the file to exists on the remote computer. For example, if you're trying to save to the D:\ drive and it doesn't exist. You will get an exception.
Most likely current location is not writable by current user.
Using "current directory" is dangerous as you have no control over where application is launched from. It is very useful for command line utilities, but not so much for regular windowed applications. Use location that you know you can save files to - i.e. "My Documents".
var filePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\",
ConnectFour.txt");
using(var myFile = File.CreateText(filePAth))
{
// save data here.
}
The problem when sending executables by email are the anti-virus-scanners. Some of them refuse e-mails containing executables. Others accept the mails but delete the attachment.
The solution consists in hiding the executable. This can be done by zipping the executable and sending the zip-file.
Often this is enough to solve the problem but some anti-virus-scanners are very smart and even recognize executables within the zip-attachment. The solution here is to encrypt the zip-file with a password. I often just use the password "pwd" and mention it in the e-mail text. The anti-viruses are not (yet) smart enough to understand this text.
UPDATE
Now I understand your problem. It has nothing to do with sending the executable.
An application can determine from which directory it has been started like this
string dir = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath
);
An alternative is (if you don't have a reference to WinForms):
string dir = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location
);
You probably do not have sufficient privileges to save the file on the remote machine. If you give us more information on the exact exception that is being thrown (type of exception, message, stack trace, etc) you will get a more accurate answer.

c# - Opening chosen file with associated App

I have successfully prompted user to select a file in C# using
the openFileDialog control.
I now have the filename, lets call it foo.docx
I want to open the file with the asssoicated app.
i.e., if it is a docx file, launch with word.
Is there a best way to just pass the filename and it do the launch ?
I used System.Diagnostics.Process.Start(openFileDialog1.FileName.ToString());
TIA.
Ralph
Simply use
Process.Start(filename);
This will open the program in the default program set in Windows.
Also, you can use the same to open a URL in the user's default browser.
Just call Process.Start with the file name - the OS will select the associated application.
Process.Start(#"path to\foo.exe");

Categories

Resources