Exemple #1
0
        //////////////////////////////////////////////////////////////////////////////////////////////
        // App resource handling


        public string GetAppResourceStr(string resourcePath)
        {
            // Note: PackageManager requirers admin privilegs

            var AppResource = TextHelpers.Split2(resourcePath.Substring(2, resourcePath.Length - 3), "?");
            var package     = packageManager.FindPackage(AppResource.Item1);

            if (package != null)
            {
                string pathToPri = Path.Combine(package.InstalledLocation.Path, "resources.pri");
                return(MiscFunc.GetResourceStr(pathToPri, AppResource.Item2));
            }

            return(resourcePath);
        }
Exemple #2
0
        private static string GetDriveLetter(string longPath)
        {
            Tuple <string, UInt64> temp;

            DriveLetterCacheLock.EnterReadLock();
            if (DriveLetterCache.TryGetValue(longPath.ToLower(), out temp))
            {
                if (temp.Item2 > MiscFunc.GetTickCount64())
                {
                    DriveLetterCacheLock.ExitReadLock();
                    return(temp.Item1);
                }
                DriveLetterCache.Remove(longPath.ToLower());
            }
            DriveLetterCacheLock.ExitReadLock();

            // ToDo: build a cache on WM_DEVICECHANGE

            string ret = null;

            char[] lpTargetPath = new char[260 + 1];
            for (char ltr = 'A'; ltr <= 'Z'; ltr++)
            {
                uint size = QueryDosDevice(ltr + ":", lpTargetPath, 260);
                if (size > 0 && longPath.Equals(new String(lpTargetPath, 0, (int)size - 2), StringComparison.OrdinalIgnoreCase))
                {
                    ret = ltr + ":";
                    break;
                }
            }

            if (ret == null)
            {
                return("?:");
            }

            DriveLetterCacheLock.EnterWriteLock();
            if (DriveLetterCache.ContainsKey(longPath.ToLower()) == false)
            {
                DriveLetterCache.Add(longPath.ToLower(), new Tuple <string, UInt64>(ret, MiscFunc.GetTickCount64() + 1 * 60 * 1000)); // cahce values for 1 minutes
            }
            DriveLetterCacheLock.ExitWriteLock();
            return(ret);
        }
Exemple #3
0
        public bool Restore(string state)
        {
            var           stateTemp = TextHelpers.Split2(state, "#"); // in case we want to add some more info
            List <string> State     = TextHelpers.SplitStr(stateTemp.Item1, "|", true);

            if (State.Count != dataGrid.Columns.Count)
            {
                return(false);
            }

            Hold = true;
            for (int i = 0; i < dataGrid.Columns.Count; i++)
            {
                var Column = dataGrid.Columns[i];
                if (State[i].Length == 0)
                {
                    Column.Visibility = Visibility.Collapsed;
                }
                else
                {
                    Column.Visibility = Visibility.Visible;

                    var PosWidth = TextHelpers.Split2(State[i], ";");

                    int Pos = MiscFunc.parseInt(PosWidth.Item1, -1);
                    if (Pos == -1)
                    {
                        continue;
                    }

                    Column.DisplayIndex = Pos;
                    Column.Width        = new DataGridLength(MiscFunc.parseInt(PosWidth.Item2, 50));
                }
            }
            Hold = false;

            CreateHeaderMenu();
            return(true);
        }
Exemple #4
0
        private UwpFunc.AppInfo GetInfo(Windows.ApplicationModel.Package package, string sid)
        {
            string path;

            try
            {
                path = package.InstalledLocation.Path;
            }
            catch
            {
                return(null);
            }

            string manifest = Path.Combine(path, "AppxManifest.xml");

            if (!File.Exists(manifest))
            {
                return(null);
            }

            XElement xelement;

            try
            {
                string manifestXML = File.ReadAllText(manifest);

                int startIndex = manifestXML.IndexOf("<Properties>", StringComparison.Ordinal);
                int num        = manifestXML.IndexOf("</Properties>", StringComparison.Ordinal);
                xelement = XElement.Parse(manifestXML.Substring(startIndex, num - startIndex + 13).Replace("uap:", string.Empty));
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
                return(null);
            }

            string displayName = null;
            string logoPath    = null;

            try
            {
                displayName = xelement.Element((XName)"DisplayName")?.Value;
                logoPath    = xelement.Element((XName)"Logo")?.Value;
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
            }

            if (displayName == null)
            {
                return(null);
            }

            try
            {
                Uri result;
                if (Uri.TryCreate(displayName, UriKind.Absolute, out result))
                {
                    string pathToPri         = Path.Combine(package.InstalledLocation.Path, "resources.pri");
                    string resourceKey1      = "ms-resource://" + package.Id.Name + "/resources/" + ((IEnumerable <string>)result.Segments).Last <string>();
                    string stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey1);
                    if (!string.IsNullOrEmpty(stringFromPriFile.Trim()))
                    {
                        displayName = stringFromPriFile;
                    }
                    else
                    {
                        string str          = string.Concat(((IEnumerable <string>)result.Segments).Skip <string>(1));
                        string resourceKey2 = "ms-resource://" + package.Id.Name + "/" + str;
                        stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey2);
                        if (!string.IsNullOrEmpty(stringFromPriFile.Trim()))
                        {
                            displayName = stringFromPriFile;
                        }
                    }
                }

                if (logoPath != null)
                {
                    string path1 = Path.Combine(package.InstalledLocation.Path, logoPath);
                    if (File.Exists(path1))
                    {
                        logoPath = path1;
                    }
                    else
                    {
                        string path2 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path1, "scale-100.png"));
                        if (File.Exists(path2))
                        {
                            logoPath = path2;
                        }
                        else
                        {
                            string path3 = Path.Combine(Path.Combine(package.InstalledLocation.Path, "en-us"), logoPath);
                            string path4 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path3, "scale-100.png"));
                            if (File.Exists(path4))
                            {
                                logoPath = path4;
                            }
                            else
                            {
                                logoPath = null;
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
            }

            return(new UwpFunc.AppInfo()
            {
                Name = displayName, Logo = logoPath, ID = package.Id.FamilyName, SID = sid
            });
        }
Exemple #5
0
        private UwpFunc.AppInfo GetInfo(Windows.ApplicationModel.Package package)
        {
            string path;
            string manifest;

            try
            {
                path = package.InstalledLocation.Path; // that call takes a long time

                manifest = Path.Combine(path, !package.IsBundle ? @"AppxManifest.xml" : @"AppxMetadata\AppxBundleManifest.xml");
                if (!File.Exists(manifest))
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }

            XElement xelement;

            try
            {
                string manifestXML = File.ReadAllText(manifest);

                int startIndex = manifestXML.IndexOf("<Properties>", StringComparison.Ordinal);
                int num        = manifestXML.IndexOf("</Properties>", StringComparison.Ordinal);
                xelement = XElement.Parse(manifestXML.Substring(startIndex, num - startIndex + 13).Replace("uap:", string.Empty));
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
                return(null);
            }

            string displayName = null;
            string logoPath    = null;

            try
            {
                displayName = xelement.Element((XName)"DisplayName")?.Value;
                logoPath    = xelement.Element((XName)"Logo")?.Value;
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
            }

            if (displayName == null)
            {
                return(null);
            }

            try
            {
                Uri result;
                if (Uri.TryCreate(displayName, UriKind.Absolute, out result))
                {
                    string pathToPri         = Path.Combine(path, "resources.pri");
                    string resourceKey1      = "ms-resource://" + package.Id.Name + "/resources/" + ((IEnumerable <string>)result.Segments).Last <string>();
                    string stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey1);
                    if (!string.IsNullOrEmpty(stringFromPriFile.Trim()))
                    {
                        displayName = stringFromPriFile;
                    }
                    else
                    {
                        string str          = string.Concat(((IEnumerable <string>)result.Segments).Skip <string>(1));
                        string resourceKey2 = "ms-resource://" + package.Id.Name + "/" + str;
                        stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey2);
                        if (!string.IsNullOrEmpty(stringFromPriFile.Trim()))
                        {
                            displayName = stringFromPriFile;
                        }
                    }
                }

                if (logoPath != null)
                {
                    List <string> extensions = new List <string> {
                        "", "scale-100.png", "scale-125.png", "scale-150.png", "scale-200.png", "scale-400.png"
                    };
                    List <string> sub_dirs = new List <string> {
                        "", "en-us"
                    };

                    string foundPath = null;
                    foreach (string extension in extensions)
                    {
                        foreach (string sub_dir in sub_dirs)
                        {
                            string testPath = path;
                            if (sub_dir.Length > 0)
                            {
                                testPath = Path.Combine(testPath, sub_dir);
                            }

                            string testName = logoPath;
                            if (extension.Length > 0)
                            {
                                testName = Path.ChangeExtension(testName, extension);
                            }

                            testPath = Path.Combine(testPath, testName);
                            if (File.Exists(testPath))
                            {
                                foundPath = testPath;
                                goto done;
                            }
                        }
                    }

done:
                    logoPath = foundPath;
                }
            }
            catch (Exception err)
            {
                AppLog.Exception(err);
            }

            return(new UwpFunc.AppInfo()
            {
                Name = displayName, Logo = logoPath, ID = package.Id.FamilyName, Path = path
            });
        }
Exemple #6
0
        public static ImageSource GetIcon(string path, double size)
        {
            string key = path + "@" + size.ToString();

            ImageSource image = null;

            IconCacheLock.EnterReadLock();
            bool bFound = IconCache.TryGetValue(key, out image);

            IconCacheLock.ExitReadLock();
            if (bFound)
            {
                return(image);
            }

            try
            {
                var pathIndex = TextHelpers.Split2(path, "|");

                if (IsImageFileName(pathIndex.Item1))
                {
                    try
                    {
                        image = new BitmapImage(new Uri(pathIndex.Item1));
                    }
                    catch { }
                }
                else
                {
                    IconExtractor extractor = new IconExtractor(pathIndex.Item1);
                    int           index     = MiscFunc.parseInt(pathIndex.Item2);
                    if (index < extractor.Count)
                    {
                        image = ToImageSource(extractor.GetIcon(index, new System.Drawing.Size((int)size, (int)size)));
                    }
                }

                if (image == null)
                {
                    if (File.Exists(NtUtilities.NtOsKrnlPath)) // if running in WOW64 this does not exist
                    {
                        image = ToImageSource(Icon.ExtractAssociatedIcon(NtUtilities.NtOsKrnlPath));
                    }
                    else // fall back to an other icon
                    {
                        image = ToImageSource(Icon.ExtractAssociatedIcon(NtUtilities.Shell32Path));
                    }
                }

                image.Freeze();
            }
            catch { }

            IconCacheLock.EnterWriteLock();
            if (!IconCache.ContainsKey(key))
            {
                IconCache.Add(key, image);
            }
            IconCacheLock.ExitWriteLock();
            return(image);
        }