clickonce wpf app application path Edit

Pooran Prasad Rajanna | 08 September 2020 | 77

Many a times we need the application path of the clickonce installed application. From the APIs we can get the datadirectory, usually is


C:\Documents and Settings\\Local Settings\Apps\2.0\Data\\\_\Data                 

But you want

C:\Documents and Settings\\Local Settings\Apps\2.0\\\_                  

truncatedappname is the first four letters of the application name and with 2 dots and the last 4 letters of the application.

So, here is the hack to get the actual path of the app.

private string GetApplicationPath()
      string strDataDirectory = ApplicationDeployment.CurrentDeployment.DataDirectory.ToString();
      string truncatedappname = "myap..tion";
      string strRoot = strDataDirectory.Substring(0, strDataDirectory.IndexOf("2.0")   4);
      string strLastFolder = strDataDirectory.Substring(strDataDirectory.IndexOf(truncatedappname));
      strLastFolder = strLastFolder.Substring(0, strLastFolder.Length – 5);
      DirectoryInfo dirFirst = new DirectoryInfo(strRoot);
      DirectoryInfo[] subdirectories = dirFirst.GetDirectories();
      foreach (DirectoryInfo dirSecond in subdirectories)
         DirectoryInfo[] subdirectories2 = dirSecond.GetDirectories();
         foreach (DirectoryInfo dirThird in subdirectories2)
               DirectoryInfo[] subdirectories3 = dirThird.GetDirectories();
               foreach (DirectoryInfo dirForth in subdirectories3)
                    if (dirForth.Name == strLastFolder)
                    return (strRoot   dirSecond.Name   "\\"   dirThird.Name   "\\"   strLastFolder   "\\");

Hope that helps