How to get selected items from window desktop? - c#

I need to obtain selected items located in a window desktop (files and folders) in current moment. I know how to obtain selected items in window explorer using shell32 object and how to get just all desktop items. But how to find out which of them are selected in current moment I don't know.
I find code snippet in question Get selected items of folder with WinAPI. It probably allows to get selected items in desktop but I think that this is hack style code and it can deliver problems on other versions of OS.
UPDATE:
I obtain solution but I have problems yet. The problem is that if I create for example 2 files, file1.txt and file1.doc and I set view option "don't show extensions of registered files" then all extensions are removed and I can't obtain what exactly file was selected because I receive only names of files (file1 and file1 without any information). Method gives results without extension. So can anyone know how to solve it?

Related

How to use windows forms resource files for localizing dynamic data

I have an application that I am developing that is made with Window Forms. For localizing all my Labels, ToolStripMenuItems, Buttons, etc I use resx resource files. Specifically to localize my application for German, I open my Main.en-CA.resx file in winres. I then go through all the terms found in the form and change them to their German translation. I then save the file to Main.de-DE.resx. I now have a Main.en-CA.resx file and a Main.de-DE.resx file. In my code I then only have to change the current culture to whatever language I want and apply the change to all my Labels, Controls, Buttons, etc. For example something like this:
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
// Must re-apply resources after changing the culture
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
resources.ApplyResources(this, "$this");
foreach (Control c in this.Controls)
{
resources.ApplyResources(c, c.Name);
}
This seems to work great for all Labels etc that do not change. I do however have entries that are changed. For example I might have a dropdown ComboBox that is filled with the entries: "Apple", "Banana", "Orange". Or I might have some error messages: "Missing Input", "Cannot find xml file" that are only sometimes displayed. Now I suppose maybe for the error messages I could just have Labels and selectively change their visibility depending on whether they need to be shown, however for the dropdown ComboBox these entries might change depending on say which file the user loads.
I am wondering then, is there a way to store these entries in the resx files and then access them from my code. I tried opening the resx files and adding them manually (i.e. without using winres) but attempting to do this resulted in the warning:
You are trying to edit a resource file that is a part of another project item (such as a form or control). Editing this item could corrupt the project item, and you will have to recover it by hand. In addition, changes made to this resource file may be lost if further changes are made to the project item.
Do you really want to edit this file?
This sounded like a bad idea so I didn't try that any further. Additionally I am not sure on how I would access the terms in the file manually. I am very new to windows forms and resource files (this is my first time using them) so I realize this might be a simple question but I have had trouble finding information on how exactly to do this.
Ok as it turns out I have uncovered how I can achieve what I am looking for. Ok from the SO post I can access any strings stored in the files Resource.resx by the code:
myLabel.Text = Properties.Resources.MissingController;
where MissingController is a key (i.e. Name) in the file Resources.resx.
Therefore all I need to do is add additional resource files such as Resource.de-DE.resx in the case of German and fill in the translations (i.e. the values in the resource file) corresponding to the same keys (i.e. the names in the resource file).
The Resources.resx file looks like:
and the Resources.de-DE.resx looks like:
As mentioned in the question I had already created some resource files for translating my forms but I had used winres. Whereas they had been located under my Main.cs [Design] file, the Resources.resx and Resources.de-de.resx are located under Properties. Because I had used winres to make my resx files I think that meant I was not supposed to manually edit them hence the warning it gave?? I'm still not 100% sure about this.
Regardless I can now just manually add terms to my Resource.resx file as well as create different versions of this file for different languages and the localization will work. When right clicking on Properties and going Add->New Item and then selecting Resource, if you do not see the Resource file type as an option (as happened to me) then that might mean you need to add the development tools that did not get installed with your version of visual studio. You can achieve this by just running the visual studio installer again and clicking modify and adding the .NET development tools.

Drag and Drop from ListView to Explorer [duplicate]

This question already has answers here:
Drag Drop from .NET application to Explorer
(2 answers)
Closed 7 years ago.
My goal is to able to drag an item from a ListView to Explorer. The way I'm trying to accomplish this is somewhat different though. From searching Google and around this site, I couldn't really find anything that would help me or pertained closely enough to what I'm trying to do. I can barely comprehend some of the examples because I don't understand enough about these drag and drop operations.
Specifically, the control that I'm trying to drag items from is a TreeListView from the ObjectListView library. The control is populated with nodes that represent the structure inside a archive-like compressed file. Entries are parsed into two different objects representing a file and a directory. Both of these models inherit an interface. When extracting these files normally, I use a Form that I instantiate with a collection of nodes and the target path that they will be extracted to as the parameters. It then takes care of extraction. This is so that I can show the overall and singular progress of each file.
Subscribing to the ItemDrag event, I know that I need to call DoDragDrop. When doing this, I need to be able to instantiate the extraction form only after the drop (when the user releases the mouse) into Explorer, and also be able to retrieve the path in which the items were dragged.
For some reason, this seems a lot more complicated than it should be. Any sort of advice or suggestions would be very helpful.
I'm not sure what format Explorer expects in the drag object, but this process might work for you.
When you begin the drag, pre-extract the selected files to some temp
directory
Build a DataObject which contains the paths of the files you have extracted in the format expected by Explorer
Use your DataObject in the DoDragDrop call
When the user drops on Explorer, the files should get copied from the temp directory to the drop target
Track all of your extracted files and cleanup when the application exists (id desired)
Hope this helps.
Edit (after some research):
I believe if you use
new DataObject(DataFormats.FileDrop, files)
where files is a string[] of the filenames to be copied (the ones you put in the temp directory), you shouold have the correct DataObject for implementation.
Haven't had a chance to write a prototype yet to test. Good Luck!

Retrieving the recommended programs for a file extension

I cannot find a solution anywhere to this problem. I've only seen bits and pieces of code that barely work to get some exe files associated in the registry to that particular extension.
What I want is the exact list the Windows Explorer context menu will give you if you go to the Open With item. If I have to do it with p/invoke that is fine, I just want the exact list that explorer would show. That would include the friendly name as well as the path to associated program.
The list I want to retrieve for a pdf:
The list I want to retrieve for a png:
It may be worth noting that Microsoft calls it the "Recommended Programs" if you attempt to choose by clicking "Choose default program..."
So how is that dialog populated? Because it must be using the same method for the Open With context menu.
There are some popular answers to this question on SO, but most of them only half work.
Example Answer #1
Example Answer #2
If the AssocQueryString function returned multiple results it'd be close to an answer, however I believe it can only get the actual program associated with it.

Get Outlook folders icons

I am building an add-in which requires selection of multiple outlook folders. For that purpose I created selection dialog that lists folders and allows user to select one or more by checking the check boxes next to tree view items.
I was hoping that somehow I could read/get the outlook folder icons from my add in code which is written in C# (any solution that works with outlook object model would do).
I was trying hard around MAPIFolder.GetCustomIcon but it returns null for all folders and when you read the documentation it is clear that it is not meant for this i.e. it returns value only if folder has custom icon and if it is not any of default folders.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder.getcustomicon%28v=office.14%29.aspx
Thanks for reading.
You'll have to use an icon editing tool (like Axialis Icon Workshop) that can extract icon and image resources from related Outlook .dll and .exe files. It's a real mess to figure out what icon is where there as they exist in small and large quantities in very many different files.

How to build a search engine in windows form that searches for folders in C drive using C#

I'm making a documentation program and I built a search engine with text boxes,combo boxes,and check list boxes. I haven't written a code that actually does the searching, there are just buttons and the components.
Anyways the files I want to access are in one folder in the C drive and in that folder there are many other folders and subfolders. I want the user to either type in the desired name of the folder,or select from a combo box.Normally we can open a folder with this code:
Process.Start(#"C:\Users\melek\Desktop\svn");
I don't want to write Users\melek etc each time in code. I need a code that grabs the users selected folder or file name(using combo boxes and text boxes) and find the folder and display it in windows form.
Is it possible to convert the selection from the text box or combo box to string and use the Process.Start command?
We solved this problem like this:
we obtained a path and used this code:
listBox1.DataSource = System.IO.Directory.GetFileSystemEntries(path1);
What you're trying to do requires several steps, so let's discuss them in turn.
First you need to get a path from the user so your program knows where to search . A FolderBrowserDialog attached to a button provides this. No need to hard code any paths since you can fetch the path string from this dialog.
Next you can use the Path, Directory and File classes to perform operations such as, fetching a list of all subfolders from a path, extracting paths of paths as strings for additional searching, and checking properties or flags on files and folders in case you decide to support more advanced search options.
I would suggest understanding these core file system classes more if you're going to be doing any amount of development that interacts with the file system. They are your bread and butter. I've linked to the MSDN documentation for each, for your convenience.

Categories