Directory right click - c#

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());
}

Related

How can I get my app to run from the Start menu?

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 + "\"");
}
}

Application not adding to startup + startup name is always "visual"

I tried adding my application to startup, if the user chooses that option. I made this code based on multiple answers on stackoverflow:
using Microsoft.Win32;
namespace Clientding
{
class Program
{
static void Main()
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rkApp.GetValue("ItsAnApp") == null)
{
MessageBox.Show("This msgbox is to confirm that the code is being run!");
rkApp.SetValue("ItsAnApp", Application.ExecutablePath.ToString());
}
}
}
}
This does add the application to my startup list, but only with the name "Visual" which should somewhere be caused by visual express.
Also, the application doesn't actually show up on startup.
INFO:
I am running windows 8.
When adding to startup, I believe I hear the sound of a connecting device, then after 2 seconds, the disconnecting sound. I am 99% sure that this has to do with the startup program.
Any ideas why this code doesn't work?
Check the restriction that denies users to run application on the Windows start:
To restrict users from running specific Windows programs by editing the registry, follow these steps:
Click Start, and then click Run.
In the Open box, type regedit, and then click OK.
Create a DWORD value named DisallowRun. To do so:
Locate and then click the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
\Policies\Explorer
On the Edit menu, point to New, and then click DWORD Value.
Type disallowrun, and then press ENTER.
Double-click the DisallowRun value that you created in the previous
step.
Type 1 in the Value data box, and then click OK.
Create a new
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
\Policies\Explorer\DisallowRun subkey. To do so:
Right-click the following registry key, point to New, and then click
Key: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
\Policies\Explorer Type disallowrun, and then press ENTER.
For each program that you want to prevent users from running, create
a new string value in the DisallowRun subkey that you created in step
Use consecutive numbers to name the string values (starting with 1), and use the executable file name for the program as the data for
the string value.
For example, if you want to restrict users from running Microsoft Internet Explorer:
Right-click the following registry key, point to New, and then click String Value:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion \Policies\Explorer\DisallowRun
Type 1, and then press ENTER.
Double-click the 1 value that you created in the previous step.
Type iexplore.exe in the Value data box, and then click OK.
Quit Registry Editor, and then restart the computer.

Context Shell Menus - Get identifier on click

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);
}

How to get the (.lnk) shortcut filepath in a program which started by the shortcut?

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];
}
}

How to get command line from a ClickOnce application?

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

Categories

Resources