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.
Related
In my C# WPF application I want to install an other program. The other program consists of a setup.exe, multiple msi files and a vcredist.exe. I need to start the setup.exe because it hands over some parameters and information to the msi files and uses an update functionality for the existing version of the program. So I can't start the msi files directly.
programPath = programPath + #"\setup.exe";
Process programsetup = Process.Start(programPath);
programsetup.WaitForExit();
Files are stored in root directory of my C# app. My problem is that I can't move the files to an subfolder because the msi files are always searched in the root directory and not in the subfolder.
now:
..\myApp\setup.exe
..\myApp\client.msi
..\myApp\host.msi
..\myApp\manager.msi
..\myApp\vcredist.exe
My question: How can I move setup.exe and msi files in a subfolder and start it from there?
What I want:
..\myApp\toolkit\setup.exe
..\myApp\toolkit\client.msi
..\myApp\toolkit\host.msi
..\myApp\toolkit\manager.msi
..\myApp\toolkit\vcredist.exe
Error I get during setup when I do it this way: ..\myApp\client.msi not found.
This code will directly launch setup.exe.
Properties -> right click Open to Resources.resx -> Top Left Add Existing File -> select the file.
byte[] resourceFile = Properties.Resources.setup;
string destination = Path.Combine(Path.GetTempPath(), "setup.exe");
System.IO.File.WriteAllBytes(destination, resourceFile);
Process.Start(destination);
I've solved it with ProcessStartInfo.WorkingDirectory.
Problem/Solution: If you start a setup file with Process which loads during installation some msi files from same directory you need to set WorkingDirectory.
Code example:
string ToolkitExe = myAppPath + #"\myApp\toolkit\setup.exe";
string ToolkitPath = myAppPath + #"\myApp\toolkit\";
ProcessStartInfo startInfo = new ProcessStartInfo(myAppPath + #"\myApp\toolkit\");
startInfo.WorkingDirectory = myAppPath;
Process p = Process.Start(startInfo);
p.WaitForExit();
RunAll();
On my project I have instructions that save Excel Files in a folder called "Ficheirs" inside the Project bin\debug directory (bin\debug\fichiers) using: Directory.GetCurrentDirectory() + "\Fichiers" to set the path to that , it works well before creating the setup
However after creating the setup using inno Setup Compiler , and eventually adding the folder "Ficheirs" while creating the setup , my program doesn't work
How to fix that so that my program keeps saving my excel files on that folder ?
Directory.GetCurrentDirectory() + "\Fichiers"
This is a very unreliable function to set a path to a directory.
Without testing, the application can work in a different directory at the time you are asking for GetCurrentDirectory().
e.g the working directory of the setup / App at the moment you run GetCurrentDirectory()
App\
App\bin\
App\bin\debug\
GetCurrentDirectory()+ "\Fichiers" returns
D:\Programs\App\Fichiers
D:\Programs\App\bin\Fichiers
D:\Programs\App\bin\debug\Fichiers
You should force at install all needed Dirs
Here is an example of a [Dirs] section:
[Dirs]
Name: "{app}\bin"
Name: "{app}\bin\debug"
Name: "{app}\bin\debug\Fichiers"
inside C#
Test before writing
try
{
// Set the current directory.
string target = Application.StartupPath + "\\bin\\debug\\Fichiers";
if (!Directory.Exists(target))
{
Directory.CreateDirectory(target);
}
// Change the current directory.
Environment.CurrentDirectory = (target);
..............
Setup iss
[Code]
var
excelPath : String;
..............
excelPath := ExpandConstant('{app}\bin\debug\Fichiers');
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'm trying to add a subdirectory to an existing path.
Win-7 64, Intel SSD
Logged into company domain as a user with admin priv, but not as "Administrator"
Visual Studio 2008, launched normally (not "Run as Admin")
Here's MyApp (WinForms, Debug, x86):
[STAThread]
private static void Main(string[] args)
{
string p = #"C:\ProgramData\MyCompany\MyApp";
Directory.CreateDirectory(p);
string f = "a.txt";
string fullPath = Path.Combine(p, f);
File.WriteAllText(fullPath, string.Empty);
Directory.Delete(p);
As expected, the last line throws IOException: "The directory is not empty". Should mean the subdir and file got created.
Problem: Windows Explorer does not show the MyApp subdir (even after refresh, type path in address bar, close/reopen, or reboot).
C:\ProgramData\MyCompany\ was created by another application's installer; Windows Explorer says its current owner is System.
Used same IDE to create a console app (Debug, x86), copied the above lines and ran; Windows Explorer was happy to show me DigitalTestApps and the file inside.
If I do any one of the following to MyApp, the problem goes away (i.e. Windows Explorer shows me the "MyApp" subdir and the file inside):
Build as x64
Launch Visual Studio using "Run as Admin"
Use any name other than "MyApp" (but I need to use MyApp for legacy reasons)
What could be causing this?
#dtb was too humble (or busy) to repost his comment (which solved my problem) as an answer, so I'll copy his comments here to make it easier for others to see how this turned out:
"Check if you can find your directory & file somewhere in C:\Users\JimC\AppData (Folder virtualization).
related: Why Virtualization on ProgramData folder in MS Vista?"
So here is what I did after reading his advice:
I deleted the virutalized directory from under C:\Users and it started working fine. Not sure how it got created in the first place, but seemingly, while it exists, attempts to create the directory where I was expecting it finds the virtualized one instead.
Thanks again dtb.