Skip to content

Commit

Permalink
Merge pull request #56 from TechieGuy12/NoInstallFix
Browse files Browse the repository at this point in the history
Installation not found fix
  • Loading branch information
TechieGuy12 authored Oct 27, 2022
2 parents c2230d3 + 9eca5b5 commit 953fdb4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 25 deletions.
70 changes: 47 additions & 23 deletions Plex/MediaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ protected virtual void OnInProgressRecordingCountChanged(int inProgressRecording
/// </summary>
private static string PlexInstallLogFile = ConfigurationManager.AppSettings["PlexInstallLogFile"];
/// <summary>
/// Plex Media Server installation log file name.
/// </summary>
private static string PlexInstallLocation = ConfigurationManager.AppSettings["PlexInstallLocation"];
/// <summary>
/// The root Plex installation folder.
/// </summary>
private static string PlexFolder = "Plex";
Expand Down Expand Up @@ -393,28 +397,48 @@ private string GetInstallPath()
{
string installPath = null;

// To avoid using the Windows Installer API, let's first check well
// known paths to find the Plex install location
List<string> defaultLocations = new List<string>();
string programFilesX86 = Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
if (!string.IsNullOrWhiteSpace(programFilesX86))
// If the install location was specified in the config file, then
// use that value
if (!string.IsNullOrWhiteSpace(PlexInstallLocation))
{
defaultLocations.Add(Path.Combine(programFilesX86, PlexFolder, PlexSubFolder));
installPath = PlexInstallLocation;
}

string programFiles = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
if (!string.IsNullOrWhiteSpace(programFiles))

if (string.IsNullOrWhiteSpace(installPath))
{
defaultLocations.Add(Path.Combine(programFiles, PlexFolder, PlexSubFolder));
// To avoid using the Windows Installer API, let's first check well
// known paths to find the Plex install location
List<string> defaultLocations = new List<string>();
string programFilesX86 = Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
if (!string.IsNullOrWhiteSpace(programFilesX86))
{
defaultLocations.Add(Path.Combine(programFilesX86, PlexFolder, PlexSubFolder));
}

string programFiles = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
if (!string.IsNullOrWhiteSpace(programFiles))
{
defaultLocations.Add(Path.Combine(programFiles, PlexFolder, PlexSubFolder));
}

foreach (string location in defaultLocations)
{
string path = Path.Combine(location, PlexExecutable);
if (File.Exists(path))
{
installPath = Path.GetDirectoryName(path);
break;
}
}
}

foreach (string location in defaultLocations)
// If the standard install paths were not in use, then try and get
// the installation path from the Plex registry key
if (string.IsNullOrWhiteSpace(installPath))
{
string path = Path.Combine(location, PlexExecutable);
if (File.Exists(path))
if (plexRegistry != null)
{
installPath = Path.GetDirectoryName(path);
break;
installPath = plexRegistry.GetInstallFolder();
}
}

Expand Down Expand Up @@ -520,14 +544,6 @@ private void Initialize()
"The Plex Media Server is not installed.");
}

InstallFolder = GetInstallPath();
if (string.IsNullOrWhiteSpace(InstallFolder))
{
throw new AppNotInstalledException(
"Plex does not appear to be installed as the Plex installation folder could not be determined.");
}
OnMessageChanged($"Plex install folder: {InstallFolder}.");

// Populate a service object with information about the Plex
// service
plexService = new ServerService();
Expand All @@ -539,6 +555,14 @@ private void Initialize()

plexRegistry = new Registry(plexService.LogOnUser);

InstallFolder = GetInstallPath();
if (string.IsNullOrWhiteSpace(InstallFolder))
{
throw new AppNotInstalledException(
"Plex does not appear to be installed as the Plex installation folder could not be determined.");
}
OnMessageChanged($"Plex install folder: {InstallFolder}.");

// Get the Plex folders
LocalDataFolder = plexRegistry.GetLocalDataFolder();
if (string.IsNullOrEmpty(LocalDataFolder))
Expand Down
26 changes: 26 additions & 0 deletions Plex/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ internal class Registry : EventSource
/// </summary>
private const string RegistryPlexDataPathValueName = "LocalAppDataPath";

/// <summary>
/// The location of the Plex installation.
/// </summary>
private const string RegistryInstallFolder = "InstallFolder";

/// <summary>
/// The user running the Plex server application.
/// </summary>
Expand Down Expand Up @@ -150,6 +155,27 @@ internal bool DeleteRunValue()
return false;
}

/// <summary>
/// Gets the location of the Plex installation folder.
/// </summary>
/// <returns>
/// The location of the Plex installation folder, otherwise <c>null</c>.
/// </returns>
internal string GetInstallFolder()
{
try
{
// Get the Plex local data folder from the users registry hive
// for the user ID associated with the Plex service
return (string)GetValue(RegistryInstallFolder);
}
catch (Exception ex)
when (ex is ArgumentNullException || ex is ObjectDisposedException || ex is SecurityException || ex is IOException || ex is UnauthorizedAccessException)
{
return null;
}
}

/// <summary>
/// Gets the local Plex data folder used by the Plex service.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("0.2.1.0")]
[assembly: AssemblyFileVersion("0.2.1.0")]
[assembly: AssemblyVersion("0.2.1.1")]
[assembly: AssemblyFileVersion("0.2.1.1")]

0 comments on commit 953fdb4

Please sign in to comment.