Process.Start can not open a file based on the extension - c#

If I write into the command prompt
c:\Data\a.xls
c:\Data\b.pdf
c:\Data\c.txt
then the corresponding files are opened with the default application. I could do the same from program.
Process.Start(#"c:\Data\a.xls");
Process.Start(#"c:\Data\b.pdf");
Process.Start(#"c:\Data\c.txt");
Unfortunately, this does not work anymore. I use windows 10 and .net7.
Process.Start("notepad.exe", #"c:\Data\c.txt"); // works
Process.Start("excel.exe", #"c:\Data\a.xls"); // does not work
If I provide the full path of excel.exe then it works. I would like to achieve the old functionality just to provide the filename and open it with the default application.

Set the UseShellExecute property to true.
The default is true on .NET Framework apps and false on .NET Core apps.
Also see StartInfo.
Download/install NuGet package: System.Diagnostics.Process
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName= #"c:\Data\a.xls", UseShellExecute = true };
Process.Start(startInfo);
Additional References:
ProcessStartInfo (source code)
ProcessStartInfo.Verbs
ProcessStartInfo.Verb
Get List of available Verbs (file association) to use with ProcessStartInfo in c#

Related

Exception when using System.Diagnostics.Process.Start to open an EML file

If you save an email to a file on disk with an EML extension, you can open it in Outlook on Windows just by double-clicking the file. I'm hoping to do something similar as my application can encounter EML files that it needs to open.
I have some crude test code that has:
using System;
using System.Diagnostics;
using System.IO;
and then:
System.Diagnostics.Process.Start(#"C:\temp\test.eml");
When I run the code I get the following exception:
System.ComponentModel.Win32Exception: 'An error occurred trying to
start process 'C:\temp\test.eml' with working directory
'C:\Users\me\source\repos\TestProj\TestProj\bin\Release\net6.0-windows'.
The specified executable is not a valid application for this OS
platform.'
I have tried placing a simple text file in the same folder and named it 'test.txt' and checked that it opens in Notepad when I double-click it, but if I try to open it with:
System.Diagnostics.Process.Start(#"C:\temp\test.txt");
I get the same error. Where am I going wrong?
There was a breaking change in .NET Core compared to .NET Framework:
Change in default value of UseShellExecute
ProcessStartInfo.UseShellExecute has a default value of false on .NET Core. On .NET Framework, its default value is true.
Change description
Process.Start lets you launch an application directly, for example, with code such as Process.Start("mspaint.exe") that launches Paint. It also lets you indirectly launch an associated application if ProcessStartInfo.UseShellExecute is set to true. On .NET Framework, the default value for ProcessStartInfo.UseShellExecute is true, meaning that code such as Process.Start("mytextfile.txt") would launch Notepad, if you've associated .txt files with that editor. To prevent indirectly launching an app on .NET Framework, you must explicitly set ProcessStartInfo.UseShellExecute to false. On .NET Core, the default value for ProcessStartInfo.UseShellExecute is false. This means that, by default, associated applications are not launched when you call Process.Start.
Use Process.Start overload accepting ProcessStartInfo and set UseShellExecute to true:
var processStartInfo = new ProcessStartInfo
{
FileName = #"C:\temp\test.eml",
UseShellExecute = true
};
Process.Start(processStartInfo);
Otherwise provide path to executable and pass path to file as a parameter:
var pathToOutlook = #"C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE";
Process.Start(pathToOutlook, #"D:\Downloads\sample.eml");

.net Process.Start Method not Launching Excel File in Excel 2013 only when launched from whithin excel com addin

System.Diagnostics.Process.Start() not launching an excel file In Excel 2013 and that too launched from inside excel comaddin.
string filename = #"C:\Users\centraluser\AppData\Roaming\STUDIO\CENTRAL\d7c98719-7aa9-4e7e-8fb6-
bd5a5b23f560\New Microsoft Excel Worksheet.xlsx";
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = filename;
var process = Process.Start(processStartInfo);
It hangs after Process.start and waits for some time and then returns with a null process value.
While the same file gets launched if I use the same code and try to open the file from a console application.
And all this behavior is only occurring with Excel 2013 only.
Kindly help on the same.
From MSDN:
Set the UseShellExecute property to specify whether to start the
process by using the operating system shell.
Set the processStartInfo.UseShellExecute to true value.

Run .net core 2 console app in Pre-build event

I have a .net core 2 project where I want to run another .net core 2 project in the pre-build events.
All my previous projects were developed using framework 4.x, which upon building generated .exe files. This .net core does not. How can I achieve the same result as the old code from my previous projects:
$(MSBuildProjectDirectory)\..\MyProjectName\bin\$(Configuration)\MyProjectName.exe
Thank you in advance !
Actually i had the same issue before then i tried another solution, run the 2nd .net
core app during the startup of the 1st one.
as we can run .net core app from command prompt if we navigated to the directory that contains (.csproj) and opened new command prompt window then run command
dotnet run
here is the code
public static void ExecuteCommand(string command, string workingDirectory)
{
var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = workingDirectory;
processStartInfo.FileName = "cmd.exe";
processStartInfo.Arguments = "/k " + command;
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = true;
Process proc = Process.Start(processStartInfo);
}
then you can call it like that
// the directory that contains (.csproj)
var appWorkingDirectory = #"D:\\Examples\\MyApp";
ExecuteCommand("dotnet run", appWorkingDirectory);
notice that will open a show the command prompt window and if you want
to hide it just set
processStartInfo.CreateNoWindow = true;
hope this can help you

.Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"

I need to let a .reg file and a .msi file execute automatically using whatever executables these two file types associated with on user's Windows.
.NET Core 2.0 Process.Start(string fileName) docs says:
"the file name does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application installed on the system."
However
using(var proc = Process.Start(#"C:\Users\user2\Desktop\XXXX.reg")) { } //.msi also
gives me
System.ComponentModel.Win32Exception (0x80004005): The specified executable is not a valid application for this OS platform.
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
with ErrorCode and HResult -2147467259, and NativeErrorCode 193.
The same code did work in .Net Framework 3.5 or 4 console app.
I can't specify exact exe file paths as the method's parameter since users' environments are variant (including Windows versions) and out of my control. That's also why I need to port the program to .Net Core, trying to make it work as SCD console app so that installation of specific .Net Framework or .NET Core version is not required.
The exception is thrown both in Visual Studio debugging run and when published as win-x86 SCD. My PC is Win7 64bit and I'm sure .reg and .msi are associated with regular programs as usual Windows PC does.
Is there solution for this? Any help is appreciated.
You can also set the UseShellExecute property of ProcessStartInfo to true
var p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Users\user2\Desktop\XXXX.reg")
{
UseShellExecute = true
};
p.Start();
Seems to be a change in .net Core, as documented here.
See also breaking changes.
You can set UseShellExecute to true and include this and your path in a ProcessStartInfo object:
Process.Start(new ProcessStartInfo(#"C:\Users\user2\Desktop\XXXX.reg") { UseShellExecute = true });
In case this bothers you as well:
For those of us that are used to simply calling Process.Start(fileName); the above syntax may give us anxiety... So may I add that you can write it in a single line of code?
new Process { StartInfo = new ProcessStartInfo(fileName) { UseShellExecute = true } }.Start();
You have to execute cmd.exe
var proc = Process.Start(#"cmd.exe ",#"/c C:\Users\user2\Desktop\XXXX.reg")
don't forget the /c
use this to open a file
new ProcessStartInfo(#"C:\Temp\1.txt").StartProcess();
need this extension method
public static class UT
{
public static Process StartProcess(this ProcessStartInfo psi, bool useShellExecute = true)
{
psi.UseShellExecute = useShellExecute;
return Process.Start(psi);
}
}
string itemseleccionado = lbdatos.SelectedItem.ToString();
var p = new Process();
p.StartInfo = new ProcessStartInfo(itemseleccionado)
{
UseShellExecute = true
};
p.Start();

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

Categories