Solved!!
I am trying to create a Custom URL Protocol of my application that will launch my application when they visit or clicked link to myapp:start
My problem is how to know where the user installed my application. Can the msi installer put the location when registering HKEY_CLASSES_ROOT to registry?
HKEY_CLASSES_ROOT
myapp
(Default) = "URL:myapp Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "myapp.exe,1"
shell
open
command
(Default) = "C:\Program Files\MyAppFolder\MyApp.exe" "%1"
I wanted to change the path "C:\Program Files\MyAppFolder\MyApp.exe" to where the user installed my app during installation process.
Solution
HKEY_CLASSES_ROOT
myapp
(Default) = "URL:myapp Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "myapp.exe,1"
shell
open
command
(Default) = "[TARGETDIR]MyApp.exe "%1""
[TARGETDIR] will automatically change to where the user installed the file e.g. "C:\Program Files\MyAppFolder\"
For more information click this link ->
Registering an Application to a URI Scheme
With the Visual Studio Setup Project, you can use the [TARGETDIR] variable when creating your registry key.
This variable is automatically set to the target directory that your application is being installed into, simply append your .exe file to the end.
Related
I develop client-server UWP app, and I want it to be deployed by sideloading (probably using WinAppDeployCmd or MDM) to the large set of devices by administrator.
But to launch, my application needs to know server IP address, which varies for each customer. I need to pass this setting somehow during deployment by administrator to every device.
Is there a way to set remote application settings? Or pass an XML config file along with appx? Or any other way to add some parameters during installation process?
Unlike e.g. MSI, there's no option to pass along "initial data" with a package for installation. One option is to install the package and then add your 'data' e.g.
Add-AppxPackage foo.appx
appdata = ApplicationDataManager.CreateForPackageFamily(foo_pkg_family)
appdata.LocalSettings.CreateContainer("x").values["y"] = z
Step 1 installs the package, most notably it will register the package for the user. That creates the package's appdata storage resources for the user. You can then use ApplicationDataManager (from a Win32/non-UWP process) to access appdata.
You may find AppData.exe handy. A simple exe using ApplicationDataManager to provide a cmdline interface to AppData. Given this or equivalent you could write a batch file
#ECHO Off
powershell -c Add-AppxPackage foo.appx
appdata.exe SET foopkgfamilyname local\configuration --value=x --type=string --data=y
As you know games on steam can be launched in browser with "steam://rungameid/".
How can I add something like that (for example myprogram://run/form1) in my C# program?
That is called a "URI Scheme" having your program open with one is as simple as adding a few registry entries in the correct places.
The documentation for creating one can be found on the msdn but for your example it should roughly look like
HKEY_CLASSES_ROOT
myprogram
(Default) = "URL:My Program"
URL Protocol = ""
DefaultIcon
(Default) = "MyProgram.exe,1"
shell
open
command
(Default) = "C:\Program Files\My Program\MyProgram.exe" "%1"
Once done following one of those "links" will launch your program and pass the uri in as a command line argument. So, for your example of myprogram://run/form1 your program would be launched as if you did from the command line:
"C:\Program Files\My Program\MyProgram.exe" "//run/form1"
I have specified a file extension to be associated with my program (Window Application) through Project Properties >> Publish >> Options >> File Associations in Visual Studio 2013.
I know that if I drag one or several files and drop it on my application (.exe), I can catch all the paths through the arguments (string[] args) of my Main method (located in Program.cs). But when I open an associated file (which launches my published and installed application), the path of the file is not passed as an argument to my Main method.
How can I catch the path of the file(s) which has launched my application?
BTW, I can also use registry (HKEY_CLASSES_ROOT) to associate file extensions with my application beside using Visual Studio's "File Associations" feature. Which method do you recommend the most and why?
Check the registry editor:
[Win+R], type "regedit"
Open HKEY_CLASSES_ROOT
Open the .yourfileextension key
In the key where your path of the executable is stored check if there's an %%1
if not rightclick and Edit it like this: "C:\Path\to\Executable\executable.exe" %%1
I actually solved my own problem with the following code:
RegistryKey command, defaultIcon, extension;
// Create Keys
command = Registry.CurrentUser.CreateSubKey(#"Software\Classes\APP NAME\shell\open\command");
defaultIcon = Registry.CurrentUser.CreateSubKey(#"Software\Classes\APP NAME\DefaultIcon");
extension = Registry.CurrentUser.CreateSubKey(#"Software\Classes\.EXTENSION");
// Create Values
command.SetValue("", "\"" + Application.ExecutablePath + "\" %1", RegistryValueKind.String);
defaultIcon.SetValue("", "ICON PATH", RegistryValueKind.String);
extension.SetValue("", "APP NAME", RegistryValueKind.String);
APP NAME is where I put the name of my application,
EXTENSION is where I put the extension I want to associate with my application, and ICON PATH is the path to the icon file which I want the associated files to have.
The %1 at the end of the ExecutablePath makes the path of the double-clicked associated file to be passed as an argument to Main method of my application.
I am trying to start a program I made in this directory:
C:\example\example.exe -someargument
when the computer starts up. I am attempting to use this registry key:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
with the key being:
Name: example
Type: REG_SZ
Data: "C:\example\example.exe -someargument"
But my program also needs files from the directory C:\example but can't find them since the current working directory is different. Is is possible to do something like this in the registry key value
"cd C:\example\; example.exe -someargument"
so that it will change the directory? Or is there a better solution?
Thanks!
You can register your application under next registry key (like this does Reg2Run tool)
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\example.exe
#="c:\example\example.exe"
Path="c:\AnotherPath"
So System.Diagnostics.Run("example.exe"); will launch your application with specified working path.
Or another way: write a launcher using C#. You can do the same using a PowerShell cmdlet.
var info = new System.Diagnostics.ProcessStartInfo(#"c:\example\example.exe", "-someargument")
{
WorkingDirectory = #"c:\AnotherPath"
};
System.Diagnostics.Process.Start(info);
At the start of the application, do the following (this is C#, convert to C++):
using System.IO;
:
:
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
You can also create a shortcut for the program in the folder and reference this shortcut in the registry:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Name: example
Type: REG_SZ
Data: "C:\example\example.lnk
If the files are always going to be in the same directory as your application, use the Application.ExecutablePath to locate the working directory for the files from within your code, then you can reference them no matter what.
If you need load DLLs from the same directory you can create subkey example.exe under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
registry key and define PATH REG_SZ value example.exe
I can launch my windows mobile application using the emulator via visual studio in the normal fashion, but i would like to run my application from the command line and preferably pass in some parameters. Is this possible?
I can launch the emulator ONLY using the command line like so:
DeviceEmulator.exe example.bin
But how can i possibly launch my application using this emulator via the command line?
Cheers
If you save the state of your emulator then you can run the created .DESS file and pick up right where the saved state left off. You can put this in a batch file:
start DeviceEmulator.exe /s "C:\Documents and Settings\user\Application Data\Microsoft\Device Emulator{E4FC2BC5-3AC4-452C-A893-AD4F273C3A7C}.dess" /nosecurityprompt /memsize 256
You'll need to change the path and file name to match your system. Here is a list of the DeviceEmulator.exe command line switches.
If you're looking for a more elegant solution, you can control DeviceEmulator.exe via a COM interface. See MSDN for more information.
--> Go to this file directory C:\Program Files (x86)\Microsoft Device Emulator\1.0\"
--> Use this command "DeviceEmulator.exe" "C:\Program Files (x86)\Windows Mobile 6.5.3 DTK\PocketPC\Deviceemulation\0409\PPC_USA_GSM_VR.BIN" /a /battery /batterycharge 100 /cpucore ARMv5 /memsize 256 /s "E:\MyCustomEmulator.dess" /skin "C:\Program Files (x86)\Windows Mobile 6.5.3 DTK\PocketPC\Deviceemulation\Pocket_PC_Phone\Pocket_PC_PE.xml" /tooltips ON /vfp false /vmname "My Custom Emulator" /z /speakerphone 7
Note:
.dess file Save the Emulator state. you can resume that saved state later.
.BIN is the actual emulator os. it is available when installing the windows mobile 6.5 sdks
.xml is the outer skin .
For more Details Visit this microsoft documentation page