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"
Related
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 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 tried to establish a process through a click button where I can do following activities.
Objective
Download the latest code from SVN.
Build 2 set of Codes to create dlls and exe-
(a)Web application in Release mode
(b)Standalone application in debug mode
Then Replace some values of keys inside config files.
Then Place them to particular location.
Steps followed so far
Created demo.bat file which will build exe and dlls for Standalone as shown below
REM * ============================Starting Setup for Standalone======================================
SET Folder= C:\Automating\Application\Source\StandaloneApp\
cd %Folder%App1
msbuild /property:Configuration=Debug App1.csproj /t:clean /t:build
cd %Folder%App2
msbuild /property:Configuration=Debug App2.csproj /t:clean /t:build
del /F /S /Q /A %Folder%Setup\*.*
XCOPY %Folder%App1\bin\Debug\*.* %Folder%Setup\*.* /S /Y /F /Q
XCOPY %Folder%App2\bin\Debug\*.* %Folder%Setup\*.* /S /Y /F /Q
Created Another bat file demo1.bat to change command prompt to VS2010 cmd prompt
%comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
%comspec% /k ""C:\Automating\BuildAuto\BuildAutomation\demo.bat""
Created one more cmd files to download from svn
TortoiseProc.exe /command:export /URL:[URL path] /Path:"C:/Automating/Demo"
Finally A web application where from user can click button to download as per svnExport.bat and build the downloaded code as per demo1.bat.
protected void Button2_Click(object sender, EventArgs e) {
ProcessStartInfo psi = new ProcessStartInfo(#"C:\AutomatingPOC\BuildAuto\BuildAutomation\demo1.bat");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = false;
Process.Start(psi);
}
Downloading event is working correctly, but build is not working. I need help on how can I build the code
Why reinventing the wheel? Use available tools, such as TeamCity and msbuild (there are plenty other alternatives as well).
I found Eugene made a really nice introduction here.
People spent man-years developing and polishing build automation tools. If I were you, I would stop right there and had a look around.
if you set psi.UserShellExecute to false, then you will need to specify that the command to execute is actually "cmd.exe" and that the batch file is an argument. You'll also have to manages the delay between the time that the request is made and the time the build is actually completed.
automating a task like this can be done easily with Auto Hot Key. also to automatically download you can use the start command and the type of browser of your choice IE Firefox iexplore chrome ~ then you can automate the download. but do note that with some websites page number may change IE.This address has a specified page code:
Try to Automate the Build Process for C# Solution through user click
so instead of putting the regular address in the batch or what ever you chose to use you can put in
https://stackoverflow.com/questions/********
allowing it to find the information
or you can use a mouse/key-board recorder to automate the task.
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.
(See end for solution)
I didn't think this was going to be hard. I have a commmand file, d:\a.cmd which contains:
copy /b d:\7zS.sfx + d:\config.txt + d:\files.7z d:\setup.exe
But these lines of C# won't execute it:
Process.Start("d:\\a.cmd");
Process.Start("cmd", "/c d:\\a.cmd");
Throws Win32Exception: "%1 is not a valid Win32 application."
Process.Start opens .pdf files...why not execute command files?
This works if I type it in a cmd window:
cmd /c d:\a.cmd
Windows XP, MS Visual Studio 2008.
Thanks in advance,
Jim
SOLUTION
I'm only SLIGHTLY embarrassed :( There was a file named cmd.exe, size zero in my app's dir. I have no idea how it got there but it is now toast and both of the above C# statements now work. I'm off to find a Harry Potter book so I can get some self-punishment ideas from Dobby...
I've got four things for you that you can try out:
(1) Try providing the full path for cmd.exe (e.g. on my machine: C:\WINDOWS\SYSTEM32\CMD.EXE).
(2) Try adding call to the command to be executed:
Process.Start(#"C:\WINDOWS\SYSTEM32\CMD.EXE", #"/c call D:\a.cmd");
(3) Besides that, I can only guess where the %1 in the Win32Exception is coming from. Maybe your file associations are set-up incorrectly.
If you type the following on the command-line:
> assoc .cmd
You will likely get a mention of cmdfile. If you then look up this token with:
> ftype cmdfile
You might get an answer along the lines of:
cmdfile="%1" %*
Those settings are stored in the registry, and this is how the command-line interpreter knows how to execute files with custom extensions. (You can find out how a PDF document is started by executing the above two statements for the .pdf extension.)
(4) If you start to suspect that your machine might be mis-configured, start regedit (the registry editor) and locate the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor.
On my Windows XP machine (and your Process.Start example works on my machine, with different filenames though), I've got the following values stored in there:
// Name Type Value
// -----------------------------------------------
// (standard) REG_SZ (not set)
// AutoRun REG_SZ
// CompletionChar REG_DWORD 0x00000040 (64)
// DefaultColor REG_DWORD 0x00000000 (0)
// EnableExtensions REG_DWORD 0x00000001 (1)
// PathCompletionChar REG_DWORD 0x00000040 (64)
Of those, the AutoRun value might be of some interest. I think it corresponds to the /d command-line switch of cmd.exe, which controls whether cmd.exe attempts to start files with custom extensions. Usually, this is enabled. Maybe, on your machine, it isn't?
Or you can do a .bat file, then call this file through System.Diagnostics.Process.Start(). It won't redirect output to Console Application, but it would certainly execute the commands inside.
You need to specify the process full name (cmd.exe).
You should try
Environment.GetFolderPath(Environment.SpecialFolder.System) + "cmd.exe"
So you can be sure to execute the right file even if a cmd.exe is in your applications directory.
It looks like something wrong with your computer. Try running this on another machine. This should work. Process.Start(string) uses ShellExecuteEx to launch the file, so it's pretty much the same thing as double-clicking the file in Explorer, as you supposed.
A simple test worked for me.
B:\foo.cmd:
#echo Hello from foo.cmd!
#pause
Program.cs:
class Program{
static void Main(){
System.Diagnostics.Process.Start("B:\\foo.cmd");
}
}
This works as expected.
Your error message is suspicious, "%1 is not a valid Win32 application." The value in my registry at HKCR\cmdfile\shell\open\command is
"%1" %*
The %1 gets replaced by the file name, and the %* can be ignored here (it indicates that any further command-line arguments should be passed along, but we're not concerned with that right now).
The fact that the file itself is launched to handle this type of file indicates that Windows itself knows how to launch this type of file. On a normal installation of Windows, the following extensions should be set up similarly:
.exe Windows and DOS executable files
.com DOS "command" files
.bat Windows and DOS batch files
.cmd Windows NT batch files
.pif Windows shortcuts to DOS executable files
If you go to HKCR\.xxx (where xxx is any of the above), the "(Default)" value should be xxxfile. If you then go to HKCR\xxxfile\shell\open\command, the "(Default)" value should be "%1" %*. Also the "(Default)" value of HKCR\xxxfile\shell should be either not set or open.
If you have anything else in any of these values, then some program has attempted to insert itself into the execution process. Viruses sometimes do this (Sircam, for example).
Have you tried executing cmd.exe, and passing the .cmd file to it as an argument?
hmm try:
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
myproc.EnableRaisingEvents=false;
myproc.StartInfo.FileName="d:\\a.cmd";
myproc.Start();
MessageBox.Show("did the command");
Have you tested your batch file in the directory, context it's going to run? The error message with %1 looks like the problem may be in there?