So here's the gist of my problem: I have a keyboard where I can assign macros and/or launch programs from. I want to include a couple Win10 and Steam applications in that list. So I opted to build an executable "launcher", so to speak.
The code is simplistic in nature. I got Steam url's to work by placing the steam url into Process.Start("steam://rungameid/#####"). I cannot, however, figure out how to get Win10 apps to work. Here's my class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process.Start(#"explorer.exe shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App");
Process.Start(#"shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App");
Process.Start("netflix://");
Application.Exit();
}
}
Each line of Process.Start() is what I've tried, to no avail.
The bottom line I attempted from this answer, which also did not work
The first line, I can put that in a Run box or from the command line saDand it will launch Netflix, but from the C# application, I get a "System cannot find the file" exception.
Thanks for any direction!
Can you please check if you have installed this app and name you enter in the Process.Start(“ ”) is correct, You can find the names when you open the registry key HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol\PackageId. Look for the CustomProperties key. It has an attribute Name. I use the below sample to open my photos, It works fine.
private void Form4_Load(object sender, EventArgs e)
{
button2_Click(null,null);
}
private void button2_Click(object sender, EventArgs e)
{
Process.Start("ms-photos://");
}
Instead of
Process.Start(
#"explorer.exe shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App"
);
Do this
Process.Start(
"explorer.exe",
"shell:AppsFolder\4DF9E0F8.Netflix_mcm4njqhnhss8!Netflix.App"
);
I was having the same problem. Currently unable to launch a windows app store application from c#. I used a work around for now. I made a bat file that navigates to the desktop and launches the desktop shortcut link. Then I call my bat file which launches the app store application.
Example of BAT file:
cd\
cd Users\d1\OneDrive\Desktop
"XYZ Games - Shortcut.lnk"
Example Code C#:
Process proc = new Process();
proc.StartInfo.FileName = "launcherXYZGames.bat";
proc.Start();
Related
Due to me having knowledge of launching apps I am aware that you have multiple ways of launching an application in C# .NET, but I'm running into a issue that occurs when attempting to launch a SDL2 application.
I have attempted the following using the Process class to:
Start the .exe file of the build.
Start the application using "cmd.exe /K" or "cmd.exe /c" followed by "exec" or "call" or "start" followed by "{path to file}" or "{path to batch file to launch the application}". Launching the application via a batch file and CMD works fine. But, whenever I attempt to even launch the application (even in a new instance of Command-Prompt launched from cmd.exe /? start cmd.exe ?params) it will yield no result.
What I can observe is that the application tries to open. It takes forever to launch into the Window mode (starting the 3D environment). After a timeout it will either, render a couple of frames of a blank window before closing or close immediately after opening the window.
So my question is, does anyone have succesfully made a launcher application for a SDL app written in C# .NET? Or knows a way to debug this behaviour? Because unfortunately, the app does not send out a error message and since SDL safely closes the application I can't observe a crash either.
Edit #1
I'm not doing anything fancy with parameters as there shouldn't be any. I already have another one functioning that launches a normal C# application as my launcher requires to open 2 programs. 1 SLD application, 1 COM:VBA controlling application.
Given:
string audioSpectrumProgram = "AudioSpectrum.exe";
string audioSpectrumBatchProgram = "AudioSpectrum.bat";
private void BtnLaunchPPTApp_OnClick()
{
//Powerpoint controlling application
pVBAApp = Process.Start(presenterProgram, $"\"{this.path}\" {this.audioFormatParams[0]} {((this.ckboxGenerate.Checked) ? "--create" : "")} lang={this.languageCodesParams[this.cboxLanguage.SelectedIndex]}");
}
Method 1:
private void BtnLaunchSDLApp_OnClick()
{
pVizualizer = Process.Start(audioSpectrumProgram); //file launched from local path (is correct)
}
Method 2:
pVizualizer = Process.Start(audioSpectrumBatchProgram); //file launched from local path (is correct)
Method 3:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
if (spectrumFileInfo.Exists)
info.Arguments = $"/c \"{spectrumFileInfo.FullName}\"";
pVizualizer = Process.Start(info);
Method 4:
based on senario of method 3. You don't have to parse arguments using ProcessStartInfo.
pVizualizer = Process.Start($"cmd.exe /K call \"{spectrumFileInfo.FullName}\"") //to observe what happens to the application
Edit #2
Not affected by changing the UseShellExecute to true or false
private void btnOpenVisualizer_Click(object sender, EventArgs e)
{
FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
info.UseShellExecute = true;
pVizualizer = new Process();
pVizualizer.StartInfo = info;
pVizualizer.EnableRaisingEvents = true;
pVizualizer.Exited += new EventHandler(myProcess_Exited);
pVizualizer.Start();
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
$"Exit time : {pVizualizer.ExitTime}\n" +
$"Exit code : {pVizualizer.ExitCode}\n"
);
}
A general way of analyzing startup issues is to use SysInternals Process Monitor.
Record the application that is not starting up properly. Use a filter for your application. Then go through all items which don't have SUCCESS in the result column. Typically you want to do that bottom-up, since the last error is the one stopping your application from loading.
Like this you'll find common startup issues like:
missing DLLs or other dependencies
old DLLs or DLLs loaded from wrong location (e.g. registered COM components)
wrong working directory, e.g. access to non-existent config files
Ok For Future reference:
Pathing to the files can be correct and everything might be in order but if you are using DLLs for imports. Change the process's working directory.
The project will run, libs can "sometimes" be found but can cause a weird unknown bug like this one. So the most optimal way of running another C# instance with SDL or any other kind of library:
private void RunSDLProgram()
{
FileInfo spectrumFileInfo = new FileInfo("pathToFile.exe");
ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.WorkingDirectory = spectrumFileInfo.DirectoryName;
pVizualizer = new Process();
pVizualizer.StartInfo = info;
pVizualizer.EnableRaisingEvents = true;
pVizualizer.Exited += new EventHandler(myProcess_Exited);
pVizualizer.Start();
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
$"Exit time : {pVizualizer.ExitTime}\n" +
$"Exit code : {pVizualizer.ExitCode}\n" +
$"output : {pVizualizer.StandardOutput}\n" +
$"err : {pVizualizer.StandardError}\n"
);
}
Running a batch file will look at it's own directory and makes all references local, but it won't alter the working directory. (already had my suspicions about changing the work directory but I didn't see a way to call 2 opperations in process.start("cmd.exe");)
I created a script to extract an executable file from the Resources to my Desktop. This works on my machine, but won't work on other's people machines because of a different username. The following script works perfectly:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes("C:\\Users\\Alex\\Desktop\\SOMETHING.exe",myfile);
}
I did some some research and found that I need to use the
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
So I compiled this script:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),myfile);
}
The problem is that it doesn't say there is an error in my code, but when I run it and press on the button I get the following error:
System.UnauthorizedAccessException Message=Access to the path 'C:\Users\Alex\Desktop' is denied.
I've tried running the code with administrator rights, but that also didn't help.
Use File.SetAttributes(myfile, FileAttributes.Normal); property before reading the file, it should work.
I'm trying to create a shortcut in my C# WinForm application for users to launch Microsoft's "Digitizer to Monitor Mapping Tool" (MultiDigiMon.exe). However it seems that even if ran as an administrator my applications or cmd processes launched from it can not find the executable in question and I have no idea why.
My Short cut consists of a simple button with the click event hooked to the fallowing function.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\System32\\cmd.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
//start.Arguments = "/K \"cd C:\\Windows\\System32 && MultiDigiMon.exe -touch\"";
Process.Start(start);
}
In my experience on Win10 systems, MultiDigiMon.exe can be always be found in C:\Windows\System32 (assuming the C drive is your root). Additionally the arguments line above was commented out to make the following screen shots as identical as possible.
On the left is the command prompt launched using the above code and on the right is a command prompt launched by typing cmd in to the OS's taskbar and and running the first result as administrator. As you can see both promps are marked as administrator but the left fails to launch MultiDigiMon.exe and won't even list the file exist using the dir command. the right seems to have no issue with either command. (MultiDigiMon.exe dose not seem to do anything at all if multiple touch screen devices are not connected and I do not believe that it give any text output especially if successful).
Finally I have also checked the permission on MultiDigiMon.exe and (with the exception of TrustedInstaller who has full control) everybody has read and execute right. Could someone explain why I can't launch MultiDigimon.exe form the left prompt or directly from my application?
As commented on by RbMm the issue was that since my application was being compiled to run on any cpu it was treated as a 32 bit application by the file system and redirected all access to the System32 directory to SysWOW64.
To get around this you can substitute "System32" width "Sysnative" as discussed here. In my case my final code ended up looking like the fallowing.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\Sysnative\\MultiDigiMon.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
start.Arguments = "-touch";
Process.Start(start);
}
Hello guys I'm new to the forum also programming and need some help about a project.
So I recently start developing a program that firstly must add its path at the end of Registry => Environment => Path.
For this job I created project (MainLogic) which contain a class (Program) that do the job, Installer Class that contains this events below and configured Setup Project. SOURCE
public InstallerClass1()
{
this.Committed += InstallerClass1_Committed;
this.Committing += InstallerClass1_Committing;
}
private void InstallerClass1_Committing(object sender, InstallEventArgs e)
{
//Console.WriteLine("");
//Console.WriteLine("Committing Event occurred.");
//Console.WriteLine("");
}
private void InstallerClass1_Committed(object sender, InstallEventArgs e)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName
(Assembly.GetExecutingAssembly().Location));
Process.Start(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\MainLogic.exe");
}
The program was installed correctly but MainLogic.exe file I call after installation cause an error and can't start. The exception is Null Reference at MainLogic.Program.Main(String[] args)
Here is a picture for better understanding -
Is there a way to avoid that exception or could you offer me another that will work.
*** Here what i found. I can execute creating and typing in to file. Writing on the console. Probably a lot of other stuff without problem. But when try to execute this peace of code which actually I have to use...
Registry.CurrentUser.OpenSubKey("Pass Key", RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("Finaly", "Done");
Registry.CurrentUser.Close();
...the exception I described above occurs. Suggestions?
So the main reason for all those "exercises" is because I want to implement ffmpeg in my application.
I guess you are hear about ffmpeg (a video/audio processing program that works in command prompt).
So what I'm working on is to implement it in my project for mp3 extracting from video files but I wanna make it more user friendly so the user can pass commands through GUI and from there my code should do the other job. So ffmpeg works through command prompt (I know there is a wrappers but I'm not very satisfied with what read about) but firstly you have to add his path to Path's value in the registry. And here's where my problem came from.
Maybe it's sounds stupid for you but you know.. when you start something make it all the way.
If course you can just add exception handling and see what goes wrong but you don`t neet that anyway. Try to set the registry key directly in your Installer
[RunInstaller(true)]
public partial class Installer1 : Installer
{
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
const string key_path = "SOFTWARE\\YourCompany\\YourApplication";
const string key_value_name = "InstallationDirectory";
RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);
if (key == null)
{
key = Registry.LocalMachine.CreateSubKey(key_path);
}
string tgt_dir = "someDirectory";
key.SetValue(key_value_name, tgt_dir);
}
if you want to alter the path enironment variables set the key there. You can simply add a new variable or look for an exiting one (including the value) for example with Registry.GetValue MSDN-Link
User Variables
HKEY_CURRENT_USER\Environment
System Variables
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
my app was working ok and it would execute on startup before.
I added a notify icon and in my code,there are some places that this icon changes.I added all required icons in the root folder of my app,and everything is working fine with the icons,except the startup boot of my app.
I can see my app's address in the "run" part of the registry(I mean everything is the same as when my app booted at startup properly).but my app won't run at startup anymore.
any advice on my matter?
PS:I thought I should explain my work a little bit and I wrote a little piece of app that has the exact same problem
public Icon[] icons = new Icon[2] { new Icon("icon1.ico"), new Icon("icon2.ico") };
public int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter %= 2;
notifyIcon1.Icon = icons[counter];
counter++;
As you can see,the app changes the icon of the notifyicon in every tick.with this code,the app won't run at startup.but if I remove the iconchanging feature of the app,it will actually run at startup
This requires psychic debugging, I'll guess that you are loading these icons using their relative path name. Something like new Icon("foo.ico").
This can only work correctly if the default working directory of your program is set where you hope it will be. It usually is, certainly when you start your program from Visual Studio or start it from a desktop shortcut. But not when you added it to the Run registry key. Environment.CurrentDirectory will be set elsewhere, typically the Windows directory.
You must always use the full path name of files. An easy way to get that path is:
var home = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var path = System.IO.Path.Combine(home, "foo.ico");
var icon = new Icon(path);
But there's certainly a better way than storing icons as files, you can embed them in your program. Project + Properties, Resources tab. Click the arrow on the Add Resource button, Add Existing File and navigate to your .ico file. Now the icon is embedded in your program, you'll never lose track of it and can't forget to copy it when you deploy your program on another machine. And the code is simpler as well:
var icon = Properties.Resources.foo;