Fix System.ArgumentException

Fix System.ArgumentException("Illegal characters in path") when parsing
path string
This commit is contained in:
bao-qian 2015-10-31 22:40:11 +00:00
parent cfeb637685
commit f3038e4fef

View file

@ -1,21 +1,21 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program.ProgramSources namespace Wox.Plugin.Program.ProgramSources
{ {
[Serializable] [Serializable]
[global::System.ComponentModel.Browsable(false)] [System.ComponentModel.Browsable(false)]
public class AppPathsProgramSource: AbstractProgramSource public class AppPathsProgramSource : AbstractProgramSource
{ {
public AppPathsProgramSource() public AppPathsProgramSource()
{ {
this.BonusPoints = -10; BonusPoints = -10;
} }
public AppPathsProgramSource(ProgramSource source) public AppPathsProgramSource(ProgramSource source) : this()
: this()
{ {
this.BonusPoints = source.BonusPoints; BonusPoints = source.BonusPoints;
} }
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
@ -33,17 +33,31 @@ namespace Wox.Plugin.Program.ProgramSources
if (root == null) return; if (root == null) return;
foreach (var item in root.GetSubKeyNames()) foreach (var item in root.GetSubKeyNames())
{ {
using (var key = root.OpenSubKey(item)) try
{ {
object path = key.GetValue(""); using (var key = root.OpenSubKey(item))
if (path is string && global::System.IO.File.Exists((string)path))
{ {
var entry = CreateEntry((string)path); string path = key.GetValue("") as string;
if (path == null) continue;
// fix path like this ""\"C:\\folder\\executable.exe\"""
const int begin = 0;
int end = path.Length - 1;
const char quotationMark = '"';
if (path[begin] == quotationMark && path[end] == quotationMark)
{
path = path.Substring(begin + 1, path.Length - 2);
}
if (!System.IO.File.Exists(path)) continue;
var entry = CreateEntry(path);
entry.ExecuteName = item; entry.ExecuteName = item;
list.Add(entry); list.Add(entry);
} }
}
key.Close(); catch (Exception e)
{
Log.Error(e.StackTrace);
} }
} }
} }