Is there a way to launch a desktop application from a Metro-style app on Windows 8? I'm trying to create some simple shortcuts to desktop applications to replace the desktop icons on the start screen, which look out of place.
I just need something super simple, preferably in C#, to open an application as soon as the app loads. I'm planning on making these shortcuts for some games, photoshop, etc, not anything I've made myself. They're also just for personal use, so I can use direct paths to applications like "C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe"
If you simply want to run a desktop application like (notepad, wordpad, internet explorer etc) then go through Process Methods and ProcessStartInfo Class
try
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\Path\To\App.exe";
p.Start();
}
// Exp 2
// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
}
On Windows 8 Metro application i discovered this: How to Start a
external Program from Metro App.
All the Metro-style applications work in the highly sand boxed
environment and there is no way to directly start an external
application.
You can try to use Launcher class – depends on your need it may
provide you a feasible solution.
Check this:
Can I use Windows.System.Launcher.LauncherDefaultProgram(Uri) to invoke another metro style app?
Ref: How to launch a Desktop app from within a Metro app?
Metro IE is a special app. You cannot invoke an executable from Metro style apps.
Try this - I have not test yet but may be it will help you..
Launcher.LaunchFileAsync
// Path to the file in the app package to launch
string exeFile = #"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
I found a solution which is suitable for me. I just made an empty textfile in my app and called it launcher.yourappyouwanttostart and then executed it with
Windows.System.Launcher.LaunchFileAsync("launcher.yourappyouwanttostart");
On the first startup it asks you for the assocation for this file and then you choose the exe file you want to run and from now on every time you execute this file, your app will be started.
I haven't actually tried if it works and it's not really a beautiful solution, but I guess Metro-style apps can launch a URI.
You could then create a desktop-program that is registered for a custom URI scheme that would then do the actual program launching.
What you can do is host external WCF service on your computer with separate installation and connect to it from metro style application using localhost. Then you can do pretty much anything including Process.Start.
I love simple things, so my solution was to use this:
Process.Start("explorer", "shell:AppsFolder\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe!App")
This will start the "new" Sticky Notes coming with Anniversary Update to Windows 10, but it works with all other "Metro" apps I tested.
To find the name of the metro app, from Windows Explorer you have to find it in shell:appsfolder using the AppUserModelId column.
Related
I have a remote computer that is always opened with a user, because I am using it as video player in which I have installed Kodi.
In this computer, I have a gRPC service installed, because I would like to can open some applications, close them and do a basic management from a mobile.
This gRPC service is hosted in a ASP Core application, that is installed as windows service. This services is running as SYSTEM user.
I am doing a test trying to open the notepad application, and it is opened, but as system user, so I can't see it in the user that is opened.
My question is, is it possible to open the notepad, or any other application, from this service and open in in the user that has the session opened to can see the notepad?
Perhaps one solution could be run the grpc service with this user, but I prefer to use the system user because some actions will need their permissions.
To open the notepad I am using this code:
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = string.Empty;
start.FileName = "notepad";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
using (Process? proc = Process.Start(start))
{
if (proc == null)
{
throw new ArgumentException("Handle error");
}
}
Thanks.
According to documentation, the ProcessStartInfo class has settable properties for UserName and Password, which seems to me like the obvious solution to your problem (I Haven't tested that myself, though).
You can even use the PasswordInClearText property though I think using a SecureString would probably be safer.
I have a very simple program which I want to be able to use multiple user interfaces with. One of those interfaces is going to be an asp netCore 3.1* web app.
For example, if the console application is launched with no parameters and the detected platform is Linux, I would like to start an AspNetCore project and launch a browser which points to the appropriate URL. Alternatively, if the console application is launched with parameters I would like the input and output to continue in a console window. I would also like to leave the possibility for other user interface types.
The result I want is that if certain conditions are met, a factory creates a web app project.
My basic approach so far has to been
WebUi.ProgramWebUi webUi = new ProgramWebUi();
Where ProgramWebUi is the class name in the Program.cs file in an aspWebApp project. This seems to create the project correctly however I also want it to launch the site using the systems default browser.
I have seen approaches for this in Windows. Those approaches being either
Process.Start("Url-Here");
Unfortunately, this doesn't work as it tries to launch an application with the name of the URL. I have also seen others for windows which involved registry lookups. However, I would like this to work cross-platform.
Is it possible to detect a systems default browser on a cross-platform basis?
Additionally, even when I manually add a path I do not seem to be able to get
Process.Start("Application Name here");
to work as expected. Even when launching something in the same working directly which is owned by the same user I am getting exceptions telling me permission is denied.
All help gratefully received.
The approach I have found which works for this is to start with an asp app, which is at its core essentially a console application anyway. Then in the program.cs (or whichever class holds your startup) remove the line
CreateHostBuilder(args).Build().Run();
and its associated entry
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
Add these to another class, which can be part of a factory pattern. This way, you can choose whether actually to start the WebUI or not. This approach also allows easy launching of multiple consoles(because it's type is a console app).
When it comes to launching a browser along with the webserver, the approach between operating systems varies. I found lots of articles stating to use Process.Start("URL-here") however I could not make this work in Linux or Windows. In Windows 10, you have to look up the information in the registry. I found a working solution for this bit here. Then use
Process process = new Process();
process.StartInfo.FileName = browserPath;
process.StartInfo.Arguments = "your-url";
process.Start();
For Linux, I tried using a similar approach using xdg-open. However, this wouldn't launch without the full path, which can vary based on distribution. Instead, the answer is to create an executable bash script and use:
Process process = new Process();
process.StartInfo.FileName = "myBashScript.sh";
process.Start();
The bash script is elementary, just two lines of code.
#!/usr/bin/env bash
xdg-open "your-url-here/"
mac users will need (this is based on reading, I do not have a Mac actually to test this)
#!/usr/bin/env bash
open "your-url-here/"
Pulling all this together. Immediately before you run the
CreateHostBuilder(args).Build().Run();
Launch a process to start the browser and add a short thread sleep to give the webserver time to initialise. Finally, get the environment and launch accordingly.
System.Threading.Thread.Sleep(5000);
Process process = new Process();
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
process.StartInfo.FileName = "./BrowserLaunch/BrowserLaunchLinux.sh";
}
else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
{
process.StartInfo.FileName = "./BrowserLaunchBrowserLaunchMacOSx.sh";
}
else if (Environment.OSVersion.Platform == PlatformID.Win32Windows ||
Environment.OSVersion.Platform == PlatformID.Win32NT)
{
string browserPath = GetPathToDefaultBrowser();
process.StartInfo.FileName = browserPath;
process.StartInfo.Arguments = "your-url";
}
else
{
//do something else
}
process.Start();
I am creating a Windows Service and using C#. I want open cmd.exe from the service. My operating system is Windows 8. Is it possible from a Windows Service, or is there another alternative for that.
(I want to open cmd.exe after some interval - that's why I chose a windows service)
This won't work. Problem is that you are trying to show UI (Console) from a Windows Service and Windows Service is not running in the context of any particular user. Starting from Vista and later OS Windows Services are running in an isolated session and are disallowed to interact with a user or desktop making it impossible to run.
Depending on what you need there are two solutions to this problem.
If you want the cmd to be opened you might consider using a task scheduled action from Windows Task Scheduler and then perform your actions
If you need just to execute some actions with the cmd.exe, for example copy file, that does not require the UI to be displayed then you can achieve that with the following
Start cmd without creating a window:
ProcessStartInfo processInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = true,
UseShellExecute = false,
FileName = "cmd.exe",
Arguments = #"/C copy /b Image1.jpg + Archive.rar Image2.jpg"
};
For the further details please check following links:
How can I run an EXE program from a Windows Service using C#?
How can a Windows Service start a process when a Timer event is raised?
I have a simple windows service which i need to use to invoke a console application.The console app generates pdf to print by opening the adobe reader window.Running the console app works fine to print pdf.But invoking it from service not successful.It doesnt even show up the console window where i log events.
Process pdfprocess = new Process();
pdfprocess.StartInfo.FileName = #"C:\Documents and Settings\xyz\Desktop\dgdfg\PdfReportGeneration\bin\Debug\PdfReportGeneration.exe";
pdfprocess.StartInfo.UseShellExecute = false;
pdfprocess.StartInfo.RedirectStandardOutput = true;
pdfprocess.Start();
But invoking other application like
pdfprocess.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
works fine.
What will be the reason?
There is probably some permissions issue there (PdfReportGeneration.exe inaccessible under service account or maybe something that it uses...)
I would advise to capture Process Monitor log to see where exactly it fails.
Windows services run in a different window station and cannot interact with the desktop, unless you're using an older version of Windows and tick a checkbox in the service properties in the service manager.
I can’t find System.Diagnostics.Process to start a new process. I guess this is on purpose. But is there a other way? Is this even possible?
You can use this reference on Windows 8 Metro application : How to Start a external Program from Metro App.
All the Metro-style applications work in the highly sand boxed environment and there is no way to directly start an external application.
You can try using Launcher class
Launcher.LaunchFileAsync
// Path to the file in the app package to launch
string exeFile = #"C:\Program Files (x86)\App.exe";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation
.GetFileAsync(exeFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launching failed
}
}
Launcher.LaunchUriAsync
Reference: Can I use Windows.System.Launcher.LauncherDefaultProgram(Uri) to invoke another metro style app?
Looks like it’s not possible to open any non-metro processes. You can open URLs or Files like *.txt, but not *.cmd or *.exe.
If there is a Custom File Association you could possibly(I haven’t try this) start a process by opening an empty file with your custom filename extension. But you can’t edit the registry to add the association from your app.
So there are no App-Only ways to do this (except not yet discovered hacks ;) ).