(How to) Use Windows file explorer to find an application? - c#

I would like to open programmaticaly an application.
First i used System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Program1.exe") works fine, but the application always needs to be on the same path (not always true because different computer)
And a simple way to find it (with mouse & click, not programmaticaly though) is to use the windows file explorer, i enter the title of my application and i find it instantly.
I would like to code that.
I thought i could use the keyboard shortcut "Home + F" and simulate the word with SendKeys.Send("blabla") but the "Home key" doesn't seem to exist with c# (at least not here https://msdn.microsoft.com/fr-fr/library/ms127847(v=vs.110).aspx)
It's a little program for children i can't expect them to find manually the path (so forget the OpenFileDialog..)
Maybe thats a very bad idea and there are another solution to find a program without knowing his path, i don't know have you got better idea?!

You're trying to solve the problem the wrong way. What you're trying to "program" is the setting oft the working directory when the program is run using Explorer. The right way to do that is to use Process.Start by passing it all of the necessary info to start the process:
var startInfo = new ProcessStartInfo("Program1.exe");
startInfo.WorkingDirectory = #"C:\Program Files (x86)\";
Process proc = Process.Start(startInfo);

Related

Why is WPF Aplication Not Launching both .exe Applications?

I have made a very simple WPF application that is intended to have a couple of options:
Option 1 is to launch a PDF file
Option 2 is to launch a couple of exe files needed to run my software.
The problem is that on the machine I have developed this on it does open both .exe files just fine, but the strange thig is that when I try this on my HP Laptop it only wants to open the unity3d exe file and not the Voice Server exe file.
Does anybody have any idea why this is the case, it's driving me nuts trying to figure it out. here is how I have by button call coded in C#
public static void LaunchKOS()
{
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var desktopPathkos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// Launch Voice Server
var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");
Process.Start(combinedPath);
// Launch Knight O.S. Unity Settings Window
var combinedPathkos = Path.Combine(desktopPathkos, "Knight O S Beta01");
Process.Start(combinedPathkos);
// Close This Window
Environment.Exit(0);
}
EDIT:
On the outside chance that for some reason it did not like doing both .exe files from one button I have even tried just launching the Voiceserver.exe from the one button call and I still get the same result where it will launch it on the machine I have developed the app on but not on any other machine despite the fact that the file folders are exactly the same??
There has to be some logical reason for why this is not working??
Anyone?!?
EDIT: More than one way to skin a cat
Well, it may not be the most elegant way to have solved the problem but I've managed to come up with a work-around.
What I did was create a shortcut of the VoiceServer.exe and then in my button code execute the shortcut and now it will open both .exe files.
It's beyond stupid that I should even have to do that, but that's what I ended up doing. :/
The folder separator for local files paths is "\" rather than "/".
Path.Combine() is defined with a params parameter, so to avoid issues with \ being treated as an escape character, it's cleaner to just pass each folder name as a separate string.
var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data", "voice-recognition-server-pc", "VoiceServer", "bin", "Release", "KittVoiceServer");

Application icon is blank when started from Process.Start

FileInfo fi = new FileInfo(fileToExcecute);
Directory.SetCurrentDirectory(fi.DirectoryName);
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = fileToExcecute;
pInfo.RedirectStandardOutput = false;
pInfo.RedirectStandardError = false;
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = true;
pInfo.WorkingDirectory = fi.DirectoryName;
if (runas)
pInfo.Verb = "runas";
pInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = Process.Start(pInfo);
The application icon is missing from the taskbar. It's just a blank square!
The above code works fine for several projects however fails with one specific program - lets call it projectX.exe. I have re-written the Main as well as startup methods of projectX.exe so that they mimic another project that displays its icon fine.
I have tried for days to discover why but have failed dismally. I have tried various ideas including changing the icon, changing it at runtime, as well as toggling whether it should be displayed or not.
If I require that projectX.exe be run as administrator, the icon displays fine but that option is not available to my clients.
Edit 20 Oct 2017
If I change the name of 'projectX.exe' to something else for example 'test.exe', then the icon shows OK in the taskbar. Where are the icons for a program stored in the registry?
Edit 22nd October 2017
After refreshing the icons as suggested, when running the program from File Explorer or creating a shortcut, the icon is no longer displayed in the taskbar.
Edit 12th November 2017
Running the program 'As Administrator', the icon displays in the taskbar as expected.
If I change the name of 'projectX.exe' to something else ... then the icon shows OK.
This is definitely an icon cache induced problem. It is not very clear why resetting it did not help to solve this problem, but it looks like you did it by hand and that has ways of not panning out correctly.
Some background. This problem is pretty common on a dev machine, programmers tend to take care of the chrome only after getting their program debugged and tested. Explorer got to see their program.exe file with the wrong icon and copied that into its cache. Changing the .exe does not force it to refresh the cached copy, arguably a bug. The cache is otherwise pretty important for Explorer, digging the icons out of the files on a folder view full of files can take any easy handful of seconds on a spindle drive.
The cache is stored in a file, not the registry. You'll find it back in c:\users\yourname\appdata\local\iconcache.db, beware that it is a hidden file. Windows 8 and up use a much fancier caching scheme with multiple icon*.db files, stored in the c:\users\yourname\appdata\local\microsoft\windows\explorer directory.
Deleting these files is enough to force Explorer to re-create them. But that doesn't necessarily come a good end, you can only be 100% sure that Explorer creates a fresh copy by terminating it before you delete the files. And other processes may have a lock on these files if they have the cache file open while you are doing this, typically because they have a shell extension loaded.
I think the best way to reset the cache is by using Ramesh Srinivasan's cleariconcache.vbs script, available from this web page. His VBScript code looks convincingly right, taking care of all the corner-cases and dutifully reporting failure. Close all running programs to give it maximum odds for success.
The issue is very hard to diagnose without a full understanding of your environment.
This does however sound like it could well be an operating system issue rather than a problem with your code.
One option might be to programatically clear the icon cache whilst restarting explorer.exe the following code should do this:
foreach (Process exe in Process.GetProcesses())
{
if (exe.ProcessName.StartsWith("iexplore"))
exe.Kill();
}
// clear icon cache
strCmdText= "del %userprofile%\appdata\local\iconcache.db /a ";
Process.Start("CMD.exe",strCmdText);
Process.Start("explorer.exe");
}
Your icon should hopefully be visible now.
We had the exact same issue, but in our case it turns out our icon was too big (in kb). Once we made it smaller in kb, the issue was resolved!

Open .exe application from .NET console application

If I try to open Notepad from a .NET console application it works fine.
I'm doing it as follows:
var p = new Process
{
StartInfo =
{
FileName = "c:\windows\system32\notepad.exe";
}
};
p.Start();
When I try to open the application I actually want to open, nothing happens. If I open that application by hand I see a Java process being created, which means it's a Java application packaged as an exe file.
Any ideas on how to open Java exe apps through .NET?
Thanks in advance
There shouldn't be much of a difference between a regular EXE and an "exified" Java application. Have you tried adjusting the working directory? Maybe there's some unzipping going on.
Lets say you had an application named "HelloWorld". Then from a command prompt (and from your code) you would launch it with:
java HelloWorld
Now this would assume that java is in your path. Which is a bad assumption. So you would be better off either having logic to populate the path to java, or to hardcode it.
I don't ever recommend hardcoding a path if you plan to pass this software around. It's never a sure thing...

Manipulating taskbar right click application menu

I need to remove Application launch and "Pin this application to taskbar" from the taskbar context menu for an application. Reason is that the application cannot start standalone, it must be fed with information from another application.
Does anyone know how?
According to this post, you can use the Windows API Code Pack but the required classes are internal. The OP said that they copied 50k lines of code to get it working. I'm not sure if it's improved since that post but here's a workaround I just thought of. Since you can only pin EXE files (and shortcuts as per comment) to the taskbar, you could rename your application to a non-exe extension (most non-exe extensions cannot be pinned).
When you want to call it from your other app, rename it to .exe, launch it, then rename it back again. For example:
Process p = new Process();
//fake extension so it can't be drag/dropped to taskbar
string fakeExtensionName = #"C:\MyFile\myProgram.test";
//what it's actually called
string exeExtensionName = #"C:\MyFile\myProgram.exe";
//rename the fake one to the real one
File.Move(fakeExtensionName, exeExtensionName);
p.StartInfo.FileName = exeExtensionName;
//launch the real one
p.Start();
//rename it back to the fake extension
File.Move(exeExtensionName, fakeExtensionName);
Anyone can rename it to an exe if they really wanted to, so your program should assume that a user can launch it directly and handle that scenario, but any file can be pinned to the taskbar by renaming it to an exe so there's no protection around that.
Ok, i found a ugly but easy solution here https://stackoverflow.com/a/3872503/1323570
apparantly the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileAssociation\AddRemoveNames contains some words that may not exist in an executable if pinning should be possible.
you can also read more here: http://www.west-wind.com/weblog/posts/2009/Oct/08/Application-that-wont-Pin-to-Taskbar-in-Windows-7
Edit:
Found the way to do it properly:
Add the key:
HKEY_CLASSES_ROOT\Applications\Example.exe\NoStartPage
ref: http://msdn.microsoft.com/en-us/library/windows/desktop/hh127439(v=vs.85).aspx

Creating a shared folder using C#

I've spent the last few days scouring the site looking for an answer to this problem,
i need to create a folder and then make it a shared networked folder, I've tried several different sample code snippets from different sites
http://www.sarampalis.org/articles/dotnet/dotnet0002.shtml (this link is dead)
but none seem to allow the folder to be shared
If anyone could be of help it'd be much aprriciated
If you don't find the real Windows API that does this, and you can settle for a dirty solution, you can do it by executing the command "net share".
For example, like this:
ProcessStartInfo info = new ProcessStartInfo("net", "share MyNewShare=c:\\folder");
info.CreateNoWindow = true;
Process.Start(info);
Note that in any case, in order to create a share you need administrative rights, so your code will have to run elevated if you're running on Win7/Vista with UAC enabled.
I did follow the link that you have in your question.
I have a Win 7 Professional + VS.NET 2010 Professional as my dev environment on my laptop.
I took the code from the article and quickly fired up a devenv and executed the code. The code executed perfectly without any error. When i looked at the created directory you dont see a share icon on the folder but it is indeed shared. How can you check that:
open a command prompt
type "net share" and press enter
you will see that it will list the folder c:\MyTestShare with a share name "My Test Share"
One more way to find out whether it is shared or not is to right click on the folder and look at the sharing tab. Check the Network Path label. It will clearly show the shared path.
Hope this helps you.
Lohith
See
http://www.sarampalis.org/articles/dotnet/dotnet0002.shtml
http://www.codeproject.com/KB/system/Share-Folder-c_.aspx

Categories

Resources