How launch batch photos/images in gallery view in C# - c#

For a project I have folders of images. An example folder might be ImagesOfDogs, and the images inside would be sequentially named (1.png, 2.png, etc.). I have a button, and when I press this button I want to open all of the images in a folder at once, in one window. I could make my own window, but I would rather use the default Windows program so the user can edit the photos. This would look like the following (just imagine that black thing is a really cute dog pic):
Let's say the path to a folder is a string titled sPathToFolderOfDogs. On my button click, I want a method like this:
private void OpenCoolPicsOfDogs()
{
Process.Start(sPathToFolderOfDogs);
}
Except that will open all enclosed files in an image viewing application, preferably whichever is the user default. I have tried:
Process.Start("PictureViewer", sPathToFolderOfDogs);
...which opens PictureViewer and does not open my photos in PictureViewer,
Process.Start(sPathToParticularImage1);
Process.Start(sPathToParticularImage2);
etc etc
...which opens each in a new window,
and possibly most creatively/desperately:
String[] arrayOfAllMyPathsToParticularImagesInTheFolder;
(put all the strings in the array)
Process.Start(arrayOfAllMyPathsToParticularImagesInTheFolder);
Which just crashed. I know opening folders in applications through Process.Start should be possible because it is done in the docs here.* Is there a way to do it with images?
*relevant code from link:
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
Thanks!

It turns out my question was a duplicate. However, I am not deleting it, as the powers that be suggest it is best to keep duplicates up as a "signpost" informing future searchers where the true path lies.
Here is the answer to my question (or more specifically, about half a dozen equally functional answers to my question).
TL;DR: The solution I am now using is from #bruceatk in the above link.

Related

How to determine path of resource files on different computers? - C#

I am writing a simple game for my course work. The application contains many pictureboxes with no images inside them. I also have a directory with needed pictures in the Visual Studio project debug folder. I need to put the pictures' paths into an array in my program to then randomly insert them into pictureboxes.
The problem is I don't know how it would work on another computer, so I can't organize all the things. The game must be launched without using Visual Studio there, only exe file. Should I first make the installation setup of my unfinished program, or something like this, and then place the application with resources somewhere on disk to know where all my pictures would be on any computer? And then maybe I could determine the exact path where I would take all the pictures and put them into the array. Or vice versa... I'm totally confused with this.
Here what I use to fill the array:
string[] spritePaths = Directory.GetFiles(/*paths*/);
Is it possible for you to distribute the pictures in a folder with the .exe file? If so, you could use a relative path to get all of the pictures.
string[] spritePaths = Directory.GetFiles("pictureFolder");
As DangerZone suggests, you could also add your images as embedded resources.
https://support.microsoft.com/en-us/help/319292/how-to-embed-and-access-resources-by-using-visual-c
Here are a few options:
Specify the path to the folder of images as a command line argument. Then you can launch it from CMD, or create a shortcut and specify the arguments there.
Add a TextBox for the user to enter the path to the images.
Add a browse button or menu button and allow the user to search for the folder using a dialog.
Use a predefined location, such as an images folder next to the exe.

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.

Open a folder in Windows Explorer and select a file works second time only

Using the code described in this answer of the SO posting "Open folder and select the file", I've created this function:
public static void OpenExplorerAndSelectFile(string filePath)
{
Process.Start(
#"explorer.exe",
string.Format(#"/select, ""{0}""", filePath));
}
This function works well, with one small issue:
Calling the function for the first time for a specific file, Windows Explorer is correctly shown with the folder of the file, but it does not select the file.
Calling the same function again for the same file, it switches back to the already open folder in Windows Explorer and then it selects the file.
E.g. the first call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") opens the folder "C:\MyFolder" in a new Windows Explorer Window. The second call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") actually activates that Window again and selects MyFile.txt.
Doing something similar in e.g. Google Chrome (Going to the download page and showing a previously downloaded file) actually works well right in the first try.
So my conclusion is that Google Chrome seems to do it a bit different than I do.
My question:
Is there a way to debug/trace the Win32/Shell method that Google Chrome calls?
I would then compare them to what I do to see the differences.
Rather than the explorer command line Chrome most probably uses the more flexible SHOpenFolderAndSelectItems Shell API.
This answer contains the required p/invoke/implementation.
Try using the shell function "SHOpenFolderAndSelectItems".

How can I create a “Open with” list as in Explore in my own application

In my application the user can select reference to file, for example a image file. I would like to make button with a arrow that opens a list with the programs installed on the system witch can open this file type.
I know that I can get the program names from the registry "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts", but how can I filter the entries out that have no meaning - "DllHost.exe, miaui.exe, etc."
And how can I open the file with program that the user choose?
# Lars Tech If I look in registry "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" and then "OpenWithList" for the extension ".jpg" I see more entries that if if rigt click on jpg file and choose Open With ( see my first images) and I only want those.
And yes there is program's entries that I properly self have added, but that have no meaning to a jpg file. And Windows can filter them out so will I.
I think you can use this article to validate and find out which of them are applicable and valid in your application
If running on Vista or better, you can use
'SHOpenWithDialog'
http://msdn.microsoft.com/en-us/library/bb762234(v=vs.85).aspx
- it's quite straightforward...
Jens

How to open files from explorer into different tabs

How to open files from explorer into different tabs. I can associate an open with menu with the file type, now when I already have the program working, how to open the new file into another tab, instead of new program.
How to find the already running process exactly, not with the name and send the filename to it.
Let me make myself clear: I want my app to be single instance, so that when user select 10 text files and press enter key, my application will open all the 10 text files into 10 tabs, instead of creating 10 processes. How to do that? How to communicate between various instances of the same process.
EDIT SOLVED: Implemented the functionality using WM_COPYDATA in C# and the SingleApplication class from codeproject.
I am not quite sure what you mean in this question. Are you trying to open Windows Explorer windows into one window with tabs? If that is the case, then I recommend you look into QT TabBar, which extends Windows Explorer to allow for such behavior.
Or perhaps you are trying to have a link open to a new tab in a web browser. If that is the case, this behavior is defined by the web browser itself. For Internet Explorer 7, you can set this behavior under Tools > Internet Options.
In the General tab, click the Settings button next to the "Tabs" section. You will want to set the "Open links from other programs in:" option to open a new tab.
Keep in mind that this behavior is defined by each user, and you can't ever make any guarantees that they will have the same browser settings as you do.
After reading your comments, I think I understand a bit better. It sounds like you want your application to only allow one instance at a time. Since you tagged this post C#, I will assume that is what you are writing your program in.
Codeproject.com has a great tutorial on how to make your program only allow a single instance.
Here is a snippet of code from their site:
static void Main()
{
if(SingleInstance.SingleApplication.Run() == false)
{
return;
}
//Write your program logic here
}
You would want to write code just before the return statement to have the existing instance open the file in a new tab.
If you are able to provide detailed information about what your program is doing, we might be able to help you with some of the specifics.

Categories