Пример #1
0
		private static string GetDefaultFileManager()
		{
			if (PlatformUtilities.Platform.IsWindows)
				return "explorer.exe";

			const string fallbackFileManager = "xdg-open";

			using (var xdgmime = new Process())
			{
				bool processError = false;
				xdgmime.RunProcess("xdg-mime", "query default inode/directory", exception =>  {
					processError = true;
				});
				if (processError)
				{
					Logger.WriteMinorEvent("Error executing 'xdg-mime query default inode/directory'");
					return fallbackFileManager;
				}
				string desktopFile = xdgmime.StandardOutput.ReadToEnd().TrimEnd(' ', '\n', '\r');
				xdgmime.WaitForExit();
				if (string.IsNullOrEmpty(desktopFile))
				{
					Logger.WriteMinorEvent("Didn't find default value for mime type inode/directory");
					return fallbackFileManager;
				}
				// Look in /usr/share/applications for .desktop file
				var desktopFilename = Path.Combine(
					Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
					"applications", desktopFile);
				if (!File.Exists(desktopFilename))
				{
					// We didn't find the .desktop file yet, so check in ~/.local/share/applications
					desktopFilename = Path.Combine(
						Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
						"applications", desktopFile);
				}
				if (!File.Exists(desktopFilename))
				{
					Logger.WriteMinorEvent("Can't find desktop file for {0}", desktopFile);
					return fallbackFileManager;
				}
				using (var reader = File.OpenText(desktopFilename))
				{
					string line;
					for (line = reader.ReadLine();
						!line.StartsWith("Exec=", StringComparison.InvariantCultureIgnoreCase) && !reader.EndOfStream;
						line = reader.ReadLine())
					{
					}

					if (!line.StartsWith("Exec=", StringComparison.InvariantCultureIgnoreCase))
					{
						Logger.WriteMinorEvent("Can't find Exec line in {0}", desktopFile);
						_defaultFileManager = string.Empty;
						return _defaultFileManager;
					}

					var start = "Exec=".Length;
					var argStart = line.IndexOf('%');
					var cmdLine = argStart > 0 ? line.Substring(start, argStart - start) : line.Substring(start);
					cmdLine = cmdLine.TrimEnd();
					Logger.WriteMinorEvent("Detected default file manager as {0}", cmdLine);
					return cmdLine;
				}
			}
		}