Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to know if there is a built-in or fastest way to get the installation path of an application. for example, i installed new software (VLC), i want to know if there is a built-in function that searches or retrieve its installation path or location of its exe. i am using visual studio 2013

What I have tried:

i'm thinking of searching through each folder in my computer, then go to each sub-folders to find the "app.exe" file.
Posted
Updated 25-Jul-16 18:38pm

Use

C#
Dim appPath As String = Application.StartupPath()

or

C#
Dim exePath As String = Application.ExecutablePath()


depending on information you are after. Have a look under Application function as there is a large number of related information available
 
Share this answer
 
Comments
ProgrammingEnthusiast 26-Jul-16 0:50am    
Application.StartupPath() and Application.ExecutablePath()
only retrieves the file path of my application. what i want to do is: to display the path of an executable file. for example, i want to display the path of an application called: sublime_text.exe

then it will display the installation path of that exe which is: C:\Program Files\Sublime Text 3
I have a situation where I need to be assured there's 'something' installed on a client machine that can display PDF's ..I dont care what that something is, ie, Acrobat, Foxit ... this technique may work, but is tied to an assumption that nothing else has 'highjacked' the extension '.pdf' in my case - in your case the extension would be iirc '.vlc'


C#
public enum AssociationStr
{
  Command = 1,
  Executable,
  FriendlyDocName,
  FriendlyAppname,
  NoOpen
}


and

C#
[Flags]
public enum AssociationFlags
{
  Init_NoRemapCLSID = 0x01,
  Init_ByExeName = 0x02,
  Init_DefaultToStar = 0x04,
  Init_DefaultToFolder = 0x08,
  NoUserSettings = 0x10,
  NoTruncate = 0x20,
  Verify = 0x40,
  RemapRunDll = 0x80,
  NoFixUps = 0x100,
  IgnoreBaseClass = 0x200
}


then with

C#
[DllImport("Shlwapi.dll". CharSet = CharSet.Auto, SetLastError = true)]
static extern uint AssocQueryString(AssociationFlags flags, AssociationStr str, string pszAssoc, string pszExtra, [Out] StringBuilder sOut, [In][Out] ref uint nOut);


you can do :-

C#
public static string GetAssociatedExecutable(string docType)
{
  string tmpAssocExe = FileInfoByExtension(AssociationStr.Executable, docType;
  return tmpAsocExe;
}

public static string FileInfoByExtension(AssociationStr assocStr, string docType)
{
  uint pcchOut = 0;
  if (AssocQueryString(AssociationFlags.Verify, assocStr, docType, null, null, ref pcchOut) != 1)
    return null;

  StringBuilder pszOut = new StringBuilder((int)pcchOut);
  if (AssocQueryString(AssociationFlags.Verify, assocStr, docType, null, pszOut, ref pcchOut) != 0)
    return null;

  return pszOut.ToString();
}


I've manually re-typed this from my dev machine thats off the network for the moment (Im typing on My Mac), so if there's any errors, do a Google for pinvoke AssocQueryString and you may get the info - I think it originally came from StackOverflow

oh, if its not obvious

C#
// Usage
string whatRunsVLC = GetAssociatedExecutable(".vlc");


(I think I split 'Executable' out into its own function because I also have a variant of it to get the DDEApplication)

[edit]
needs
C#
using System.Runtime.InteropServices;
where you put 'the good bits' of this [/edit]
 
Share this answer
 
v2
XML
Application.StartupPath() and Application.ExecutablePath()
only retrieves the file path of my application. what i want to do is: to display the path of an executable file. for example, i want to display the path of an application called: sublime_text.exe

then it will display the installation path of that exe which is: C:\Program Files\Sublime Text 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900