Before publishing I went to Project -> Properties -> Options -> File Associations and added the extension ".hsp". Set an icon and a ProgID ("MyCompany.Document.1" for testing). After I published and installed, my .hsp files had the icon I set, so the file association should be properly set, but when I double clicked one of these files the application run and I expected the name of the file I double clicked to be in the command line. I tried reading the parameter passed to my Main function, tried Environment.CommandLine, and tried Environment.GetCommandLineArgs(), but the only thing I found was the application path. By the way I'm doing all this check before creating my main form in the Main function, just to test. The args parameter is empty and the other two only contain my app path.
This is the beginning of my Main function:
static void Main(string[] args)
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
MessageBox.Show("CommandLine -> " + Environment.CommandLine);
foreach (string str in args) MessageBox.Show("args -> " + str);
foreach (string str in Environment.GetCommandLineArgs()) MessageBox.Show("GetCommandLineArgs -> " + str);
When you publish an app with ClickOnce and then launch it by double-clicking an associated file, the path to that file actually gets stored here:
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]
See MSDN's documentation for it here:
http://msdn.microsoft.com/en-us/library/system.runtime.hosting.activationarguments.aspx
Plus a tutorial on adding file associations to "Published" projects:
http://blogs.msdn.com/b/mwade/archive/2008/01/30/how-to-add-file-associations-to-a-clickonce-application.aspx
Related
I'm designing a C# WinForms program that when the user right clicks on a directory and selects the item which I added to shell context menu (which opens the .exe for my application), it runs in the background based on where the user right clicks.
I've already figured out how to install it and add it to the correct context menu, but I can't seem to figure out the most crucial part of the program. I've already looked here, but that doesn't answer my question and the answer it gives just leads to another question.
I also realize that command line arguments exist, and that is how this question is answered. When I go onto Microsoft's website about using command line arguments, it is only about using an actual command line, which I am not using.
So my question is:
How exactly do I get the directory path when a user right clicks a folder and choose the shell context menu which I added?
If I have to use a command line in the background, that is fine I just need to be able to get and send the directory path to my program.
Here is relevant code for how I use the entered directory. In essence source is the directory path that I want when the user right clicks.
private void recursiveCheck(string source)
{
string[] directories = Directory.GetDirectories(source);
foreach(string directory in directories)
{
string test = new DirectoryInfo(directory).Name;
if (test.Length >= 3 && (test.Substring(test.Length - 3).Equals("val", StringComparison.InvariantCultureIgnoreCase) || (test.Substring(test.Length - 3).Equals("ash", StringComparison.InvariantCultureIgnoreCase)))
{
if (Directory.Exists(directory + "\\STARTUP"))
testing_dir(directory);
else
{
MessageBox.Show("Error! Startup folder does not exist in: " + test);
Application.Exit();
}
}
else
recursiveCheck(directory);
}
}
I assume you have added your application to the context menu of folders in registry:
HKEY_CLASSES_ROOT
Directory
shell
OpenWithMyApp → (Default): Open With My App
command → (Default): "c:\myapp.exe" "%V"
The key point is in %V. It will be the folder name which you right clicked on it and it will be passed to your application as command line argument.
Then in your application, it's enough to have something like this:
[STAThread]
static void Main()
{
string folderName = null ;
if (Environment.GetCommandLineArgs().Length > 1)
folderName = Environment.GetCommandLineArgs()[1];
MessageBox.Show(folderName);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new Form1());
}
If you type "regedit" in the Start menu's edit box and mash the Enter key, Registry Editor will be invoked. The same is true for "cmd" and the Command Line, and doubtless several other apps.
How can I get my app to respond the same way, so that if the user enters "Platypus" in the Start menu edit box, Platypus.exe will be invoked?
Does it require manipulation of the Registry / adding an entry somewhere there, and if so, just what key and value needs to be added?
I would be satisfied with the user needing to run the app manually once (2-clicking its icon; it's a Winforms app), at which time startup code (no pun intended) would do whatever was necessary to make the app henceforth Startsmartable (Windows key, "Platypus", to start the app).
I know that it's just as easy/easier for the user to simply 2-click a desktop icon when they want to run the app, but this particular functionality is not my idea, so complaints about the oddity of this question would be to no avail.
UPDATE
I added the code recommended by Chandan (with my executable's name):
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("RoboReporter", "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
}
}
...called it from the main form's load event:
private void FormRoboReporter_Load(object sender, EventArgs e)
{
RoboReporterConstsAndUtils.AddToStartup();
}
...shut down the app, went to the Start menu and entered the program's name ("RoboReporter"), and all it did was bring up search results of related file names.
UPDATE 2
What it does do is cause my app to run whenever the computer is restarted. That's not what I want. The code above adds an entry to HKEY_CURRENT_USER.Software.Microsoft.Windows.CurrentVersion.Run as can be seen here (along with a couple of other entries that predated it):
I don't want the app to start up every time the computer restarts, so I removed the entry. The question remains: how can I make the app runnable from the Start menu?
You can add your application's parent directory's path to the environment variable called PATH.
string pathvar = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", pathvar + ";" + Application.StartupPath + "\\", EnvironmentVariableTarget.Machine);
(Note that the paths added to this variable should end with a backslash \, and each path is separated by a semicolon ;)
Adding the parent directory's path to the environment variable will make all it's contents quickly accessible from the Start Menu's search field, from Run and from CMD.
You can also change EnvironmentVariableTarget.Machine to EnvironmentVariableTarget.User to modify the variable for the current user only.
EDIT:
A note: Setting a variable for the entire machine (by using EnvironmentVariableTarget.Machine) seems to require elevated privileges when done from one's application.
you might want to run this
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("Name_of_your_Program", "\"" + Application.ExecutablePath + "\"");
}
}
I have created a context shell menu for / on .txt files.
Its 'action' is similar to that of 'Edit with notepad' option.
I am able to open 'notepad' on clicking the menu using this code -
subKey.SetValue("", "C:\\Windows\\System32\\notepad.exe");
//subKey is the newly created sub key - The key creation part works fine.
How will I be able to use a feature similar to that of the 'Edit with notepad' feature? Or is it at least possible to get the name of the '.txt' file on which this event was triggered?
Note: By 'Edit with notepad', I mean viewing the selected file's contents in notepad.
The shell (explorer.exe) will substitute %1 with the file name. So you basically write:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad]
#="Open with &Notepad"
[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad\command]
#="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""
The file name will be passed to C:\Windows\System32\notepad.exe as a command line argument. For example if you open D:\blah.txt, then notepad will receive D:\blah.txt as the first argument.
In C#, you basically use either Environment.GetCommandLineArgs() or args in Main to retrieve the file path.
An example:
string[] commandLineArgs = Environment.GetCommandLineArgs();
string fileToOpen = null;
if (commandLineArgs.Length > 1)
{
if (File.Exists(commandLineArgs[1]))
{
fileToOpen = commandLineArgs[1];
}
}
if (fileToOpen == null)
{
// new file
}
else
{
MyEditor.OpenFile(fileToOpen);
}
I have a c# program which open *.postfix file.
If a user runs a (.lnk)shortcut which points to my type of file, my program will open the target.
So, how could my program know it is started by a (.lnk)shortcut (and get it's file path)?
In some circumstances,i need to replace the .lnk file.
Thanks!
Edited
First, thanks to guys who answered my question.
By following #Anders answer, i find out my problem lays here.
I made some changes to windows registry, so browser knows to throw customized protocol string to certain program.
some thing like this..
[InternetShortcut]
URL=myProtocol://abcdefg.....
That's maybe why i lost lpTitle. :(
I'm going to try this way:
Whenever my program invoked, of course fed with %1, program checks current opened explorer(Window), and try to get it's current path with IWebBrowserApp. With that path and desktop of course, scan and analyze *.lnk to determine which one to replace.
I think this will probably work, but not be sure. I will try.
continued
In native code you can call GetStartupInfo, if the STARTF_TITLEISLINKNAME bit is set in STARTUPINFO.dwFlags then the path to the .lnk is in STARTUPINFO.lpTitle. I don't know if there is a .NET way to get this info, you probably have to P/Invoke...
You don't. There's no way to do it. End of story.
So this has been brought to my attention due to a recent downvote. There's an accepted answer showing an idea that gets the path to the launching shortcut most of the time. However my answer is to the whole. OP wants the link to the shortcut so he can change it. That is what can't be done most of the time.
Most likely case is the shortcut file exists in the start menu but is unwritable. However other cases involve the shortcut coming from another launching application that didn't even read it from a disk but from a database (I've seen a lot of corporate level restricted application launch tools). I also have a program that launches programs from shortcuts not via IShellLink but by parsing the .lnk file (because it must not start COM for reasons) and launching the program contained. It doesn't pass STARTF_TITLEISLINKNAME because it's passing an actual title.
If you're using Visual Studio Setup Project to build an installer and do the file type association, you should follow these instructions http://www.dreamincode.net/forums/topic/58005-file-associations-in-visual-studio/
Open up your solution in Visual studio.
Add a Setup Project to your solution by file , add project,New project, Setup & Deployment projects,Setup project
Right-click on your setup project in the "Solution Explorer" window,Select view,then select file types.
you'll see the "file types" window displayed in Visual studio.At the top of the window will be "File types on target machine"
Right-click on "File types on target machine".the menu will pop up with Add "file type" Click on this.
you'll see "New document Type#1" added,and "&open"underneath it.
The "new document type#1" can be anything you want - change it to something descriptive.although the user never sees this,never use something common- be as unique as possible,Because you can overlay current file associations without even realizing it.For example,you might think"pngfile" might be a useful name- but using that will now send all"*.png" files to your application,instead of to an image viewer.A good practice maybe "YourCompantName.Filetype",where your company name is your name of your company's name, and "Filetype" is a descriptive text of your file.
In the "properties" window for your new type,you will need to change a few properties.:
Command:Change to the application that you want to run.If you click on the "..." and you will proberly want to locate and use the "primary Output..." File
Description: This is the description of the file type(if it doesn't describe it's self"
Extensions:This your list of extensions for you chosen Program.Separate each one with a ","
Icon:This will associate the icon with your file type,This shows up in the window explorer.
Now we move to that "&open ".This is an action that is available if your right-click on the file.The default action("&Open" is currently set as the default) is what happens when you double click on the file.Right click on your "New document type#1" to add actions,but for the moment,lets define our "&open" action
Click on "&Open".You will see in the properties window "Name","Arguments","Verbs". Verb is hidden from the user,but is the key that is stored in the registry.Leave it same as the name,But without the "&".The default for"Arguments" is "%1",Which means to pass the full path and filename to your application.You can add other stuff here as well,if you need to pass flags to your application to do special stuff.All this infomaton is getting passed to your application on the command line,so you'll need to be familiar with the "Environment.CommandLine" object.
If you need to set a different action as your default,just right click on the action and "set as default"
Basically, you'll pass the file path as an argument to your program. Then if it's a console application or Windows Forms , you should check the arguments in Program.Main
static void Main(string[] args)
{
//if file association done with Arguments %1 as per forum post above
//you file path should be in args[0]
string filePath = null;
if(args != null && args.Length > 0)
filePath = args[0];
}
For a WPF application you'll need to handle that in the StartUp event for your Application
void App_Startup(object sender, StartupEventArgs e)
{
string filePath = null;
if ((e.Args != null) && (e.Args.Length > 0))
{
filePath = e.Args[0];
}
}
Is it possible to make an application in C# that will be able to delete itself in some condition.
I need to write an updater for my application but I don't want the executable to be left after the update process.
There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself.
George.
[EDIT]
In more details:
I have:
Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself.
Application executes as folowed:
Application: Start -> Check Version -> Download new Updater -> Start Updater -> exit;
Updater: Start -> do it's work -> start Application Executable -> self delete (this is where I get stuck);
If you use Process.Start you can pass in the Del parameter and the path to the application you wish to delete.
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);
Code snippet taken from this article
I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards
public static class Updater
{
public static void Main()
{
string path = #"updater.bat";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("updater.exe");
sw.WriteLine("delete updater.exe /y");
sw.WriteLine("delete updater.bat /y");
}
System.Process.Start(path);
}
else
{
RunUpdateProcess();
}
}
private void RunUpdateProcess()
{
.....
}
}
It's tricky without introducing yet another process (that you'd then want to delete as well, no doubt). In your case, you already have 2 processes - updater.exe and application.exe. I'd probably just have the Application delete updater.exe when it's spawned from there - you could use a simple command line arg, or an IPC call from updater.exe to application.exe to trigger it. That's not exactly a self deleting EXE, but fulfills the requirements I think.
For the full treatment, and other options you should read the definitive treatment of self deleting EXEs. Code samples are in C (or ASM), but should be p/invokable.
I'd probably try CreateFile with FILE_FLAG_DELETE_ON_CLOSE for updater.exe with something like (psuedo code):
var h = CreateFile(
"updater.exe",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE
);
byte[] updaterBytes = GetUpdaterBytesFromWeb();
File.WriteAllBytes("updater.exe", updaterBytes);
Process.Start("updater.exe");
Once application.exe exits, updater.exe has a file handle of 1. When updater.exe exits, it drops to 0 and should be deleted.
Couldn't you simply delete the updater from within the application? i.e.:
Application: Start -> [Delete old updater if present] -> Check version -> Download new updater -> Start updater -> exit;
Updater: Start -> Perform update -> Start application -> exit;
Application: Start -> [Delete old updater if present] -> ...
Mhh so let me get this straight. You got some application.exe and your updater application updater.exe?
So when you start your application.exe it checks some webserver for a newer version and then starts updater.exe. And you want updater.exe to delete itself after it has finished updating? Or do you want to delete the downloaded patch (or similar)? Please be a bit more precise.
Consider that when you are deleting updater.exe you must recreate it for the next update process.
your second line can be
Updater: Star -> do it's work -> start Application Executable -> Updater Exits -> Application deletes your Updater.exe
public void uninstall() {
string app_name = Application.StartupPath + "\\" + Application.ProductName + ".exe";
string bat_name = app_name + ".bat";
string bat = "#echo off\n"
+ ":loop\n"
+ "del \"" + app_name + "\"\n"
+ "if Exist \"" + app_name + "\" GOTO loop\n"
+ "del %0";
StreamWriter file = new StreamWriter(bat_name);
file.Write(bat);
file.Close();
Process bat_call = new Process();
bat_call.StartInfo.FileName = bat_name;
bat_call.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
bat_call.StartInfo.UseShellExecute = true;
bat_call.Start();
Application.Exit();
}
self delete by an external executable file ".bat" for windows form applications.