diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index 4cbc737..6d6ed58 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -120,9 +120,6 @@ static void Main(string[] args) //Regex regex = new Regex(@"[\\]([A-Za-z0-9]+)$"); //Regex regex = new Regex(@"^(?\w+)[\\]"); - var biosquery = RegistryHive.LocalMachine.OpenView64(@"HARDWARE\DESCRIPTION\System\BIOS", false); - var BiosMajorRelease = biosquery.GetValue("BiosMajorRelease"); - var iiu = Convert.ToUInt32(BiosMajorRelease); var queryreg = RegistryHive.LocalMachine.OpenView64(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A", false); @@ -142,28 +139,30 @@ static void Main(string[] args) - var rr = queryable.GroupBy(x => x.GetValue("DisplayName"), (x, y) => new { x, y = y.Select(xuu => new InstalledApp() { }) }); - + //var rr = queryable.GroupBy(x => x.GetValue("DisplayName"), (x, y) => new { x, y = y.Select(xuu => new InstalledApp() { }) }); + var rr = queryable.Select(x => x.Name.GetLastSegement()); var ttype = rr.GetType(); MethodCallExpression methodcall = rr.Expression as MethodCallExpression; - var unary = methodcall.Arguments[2] as UnaryExpression; - var lambda = unary.Operand as LambdaExpression; - - - var newexpr = lambda.Body as NewExpression; - methodcall = newexpr.Arguments[1] as MethodCallExpression; - var ggw = methodcall.Method.GetGenericArguments(); - lambda = methodcall.Arguments[1] as LambdaExpression; - ttype = methodcall.Arguments[1].GetType(); - - var bios_reg = new RegQuery() - .useSetting(x => - { - x.Hive = RegistryHive.LocalMachine; - x.SubKey = @"HARDWARE\DESCRIPTION\System\BIOS"; - x.View = RegistryView.Registry64; - }); - var bios = bios_reg.Dump(); + //var unary = methodcall.Arguments[2] as UnaryExpression; + //var lambda = unary.Operand as LambdaExpression; + + + //var newexpr = lambda.Body as NewExpression; + //methodcall = newexpr.Arguments[1] as MethodCallExpression; + //var ggw = methodcall.Method.GetGenericArguments(); + //lambda = methodcall.Arguments[1] as LambdaExpression; + //ttype = methodcall.Arguments[1].GetType(); + + //var direct_reg = new RegQuery() + // .useSetting(x => + // { + // x.Hive = RegistryHive.LocalMachine; + // x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\2A"; + // x.View = RegistryView.Registry64; + // }); + //direct_reg.CreateOrUpdate(new A2() { A = "12345" }); + //var bios = direct_reg.Get(); + //direct_reg.Delete(); var regt = new RegQuery() .useSetting(x => @@ -180,12 +179,14 @@ static void Main(string[] args) x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A"; x.View = RegistryView.Registry64; }); + int remove_count2 = regt.Where(x => x.Version == new Version(1, 1, 1, 1)).RemoveAll(); //var yj = regt1.Take(2); //var aaaa = regt.Except(regt1.Take(2)); //var aaaa = regt.Where(x=>x.DisplayName!="").Except(regt1.Take(2)); //var aaaa = regt.Except(installs, new InstallAppCompare()); - var aaaa = regt.Except(regt.ToList().Take(2)); + //var aaaa = regt.Union(regt.ToList().Take(2)); //var aaaa = regt.Select(x => x).Where(x => x.DisplayName != ""); + var aaaa = regt.Where(x => x.Key == $"{x.Key}"); foreach (var oo in aaaa) { @@ -504,6 +505,11 @@ public AppData(string name) public bool IsOfficial { set; get; } } + public class A2 + { + public string A { set; get; } + } + public class BIOS { [RegSubKeyName] diff --git a/QSoft.Reg/RegAttribute.cs b/QSoft.Reg/RegAttribute.cs index 29a79b3..40c96b7 100644 --- a/QSoft.Reg/RegAttribute.cs +++ b/QSoft.Reg/RegAttribute.cs @@ -21,5 +21,6 @@ public class RegPropertyName : Attribute [AttributeUsage(AttributeTargets.Property)] public class RegSubKeyName:Attribute { + public bool IsFullName { set; get; } = false; } } diff --git a/QSoft.Reg/RegExpressionVisitor.cs b/QSoft.Reg/RegExpressionVisitor.cs index e5834c0..a5add81 100644 --- a/QSoft.Reg/RegExpressionVisitor.cs +++ b/QSoft.Reg/RegExpressionVisitor.cs @@ -360,25 +360,28 @@ protected override Expression VisitMember(MemberExpression node) else { var regexs = typeof(RegistryKeyEx).GetMethods().Where(x => "GetValue" == x.Name && x.IsGenericMethod == true); - //if (node.Member.GetCustomAttributes(typeof(RegIgnore), true).Length > 0) - //{ - // this.Fail = $"{node.Member.Name} is ignored, please do not use"; - //} - var regname = node.Member.GetCustomAttributes(true).FirstOrDefault(); + + var attr = node.Member.GetCustomAttributes(true).FirstOrDefault(); Expression member = null; Expression left_args_1 = null; - if(regname is RegIgnore) + if (attr is RegIgnore) { this.Fail = $"{node.Member.Name} is ignored, please do not use"; } - else if (regname is RegPropertyName) + else if (attr is RegPropertyName) { - left_args_1 = Expression.Constant((regname as RegPropertyName).Name); + left_args_1 = Expression.Constant((attr as RegPropertyName).Name); member = Expression.Call(regexs.ElementAt(0).MakeGenericMethod(node.Type), exprs.ElementAt(0).Value, left_args_1); } - else if(regname is RegSubKeyName) + else if (attr is RegSubKeyName) { + var subkeyname = attr as RegSubKeyName; member = Expression.Property(exprs.First().Value, "Name"); + if(subkeyname.IsFullName == false) + { + var method = typeof(RegQueryHelper).GetMethod("GetLastSegement"); + member = Expression.Call(method, member); + } } if(member ==null&&left_args_1 == null) { @@ -577,25 +580,26 @@ protected override Expression VisitMethodCall(MethodCallExpression node) { switch(expr.Method.Name) { - case "Except": - { - var pps = expr.Method.GetParameters(); - var vv = typeof(RegQueryEx).GetMethods().Where(x => - { - bool result = false; - if(x.Name == $"{expr.Method.Name}_RegistryKey" && x.GetParameters().Length == pps.Length) - { - result = true; - } - return result; - }).First(); - ttypes1 = exprs1.Select(x => x.Value).GetTypes(vv); - methodcall = Expression.Call(vv.MakeGenericMethod(ttypes1), exprs1.Select(x => x.Value)); - } - break; //case "Except": - case "Union": + //case "Intersect": + // { + // var pps = expr.Method.GetParameters(); + // var vv = typeof(RegQueryEx).GetMethods().Where(x => + // { + // bool result = false; + // if(x.Name == $"{expr.Method.Name}_RegistryKey" && x.GetParameters().Length == pps.Length) + // { + // result = true; + // } + // return result; + // }).First(); + // ttypes1 = exprs1.Select(x => x.Value).GetTypes(vv); + // methodcall = Expression.Call(vv.MakeGenericMethod(ttypes1), exprs1.Select(x => x.Value)); + // } + // break; + case "Except": case "Intersect": + case "Union": case "Distinct": { var sd = typeof(TData).ToSelectData(); diff --git a/QSoft.Reg/RegProvider.cs b/QSoft.Reg/RegProvider.cs index 8403b5f..ac32098 100644 --- a/QSoft.Reg/RegProvider.cs +++ b/QSoft.Reg/RegProvider.cs @@ -163,7 +163,6 @@ public IEnumerable Enumerable(IQueryable query) bool m_IsWritable = false; IQueryable m_Regs; - //List> m_Temps = new List>(); public IQueryable CreateRegs() { if (this.m_Regs?.Count() > 0) @@ -176,15 +175,12 @@ public IQueryable CreateRegs() } List regs = new List(); - RegistryKey reg = this.Setting; + RegistryKey reg = this.Setting.Create(); var subkeynames = reg.GetSubKeyNames(); foreach (var subkeyname in subkeynames) { regs.Add(reg.OpenSubKey(subkeyname, m_IsWritable)); } - //var qqr = regs.AsQueryable(); - //m_Temps.Add(qqr); - //return qqr; return m_Regs = regs.AsQueryable(); } @@ -239,7 +235,6 @@ public TResult Execute(Expression expression) Expression arg2 = updatemethod.Arguments[1]; if(type3[1] == typeof(TData)) { - //arg2 = this.ToSelectData(); arg2 = typeof(TData).ToSelectData(); } updatemethod = Expression.Call(oo, updatemethod.Arguments[0], arg2, typeof(TData).ToSelectData()); @@ -314,33 +309,6 @@ public TResult Execute(Expression expression) if (excute_reg != null) { inst = excute_reg.ToFunc()(excute_reg); - //var pps = typeof(TResult).GetProperties().Where(x => x.CanWrite == true&&x.GetCustomAttributes(typeof(RegIgnore), true).Length==0); - //inst = Activator.CreateInstance(typeof(TResult)); - //foreach (var pp in pps) - //{ - // var regattrs = pp.GetCustomAttributes(true); - // string subkeyname = pp.Name; - // object yyy = null; - // if(regattrs.Length>0) - // { - // var regattr = regattrs.FirstOrDefault(); - // if(regattr is RegSubKeyName) - // { - // yyy = excute_reg.Name; - // } - // else if(regattr is RegPropertyName) - // { - // subkeyname = (regattr as RegPropertyName)?.Name; - // yyy = regexs.ElementAt(0).MakeGenericMethod(pp.PropertyType).Invoke(excute_reg, new object[] { excute_reg, subkeyname }); - // } - // } - // else - // { - // yyy = regexs.ElementAt(0).MakeGenericMethod(pp.PropertyType).Invoke(excute_reg, new object[] { excute_reg, subkeyname }); - // } - // pp.SetValue(inst, yyy, null); - //} - } else { @@ -354,8 +322,6 @@ public TResult Execute(Expression expression) oo.Dispose(); } } - - return_hr = (TResult)inst; } diff --git a/QSoft.Reg/RegQuery.cs b/QSoft.Reg/RegQuery.cs index bb730ee..ee2ae06 100644 --- a/QSoft.Reg/RegQuery.cs +++ b/QSoft.Reg/RegQuery.cs @@ -53,11 +53,51 @@ IEnumerator IEnumerable.GetEnumerator() throw new NotImplementedException(); } - public T Dump() - { - RegistryKey reg = (this.Provider as RegProvider)?.Setting; - return reg.ToFunc()(reg); - } + //public void CreateOrUpdate(T data) + //{ + // var setting = (this.Provider as RegProvider)?.Setting; + // RegistryKey reg = setting?.Create(true); + // if (reg == null) + // { + // Regex regex1 = new Regex(@"^(?.+)(?<=\\)(?.*)", RegexOptions.Compiled); + // string subkey = setting.SubKey; + // var match = regex1.Match(subkey); + // if(match.Success) + // { + // var basekey = RegistryKey.OpenBaseKey(setting.Hive, setting.View); + // var parentreg = basekey.OpenSubKey(match.Groups["parent"].Value, true); + // reg = parentreg.CreateSubKey(match.Groups["path"].Value, RegistryKeyPermissionCheck.ReadWriteSubTree); + // } + // } + + + // if(reg != null) + // { + // var pps = typeof(T).GetProperties().Where(x => x.CanRead == true); + // foreach(var pp in pps) + // { + // reg.SetValue(pp.Name, pp.GetValue(data, null)); + // } + // } + //} + + //public T Get() + //{ + // RegistryKey reg = (this.Provider as RegProvider)?.Setting?.Create(); + // return reg.ToFunc()(reg); + //} + + //public void Delete() + //{ + // RegistryKey reg = (this.Provider as RegProvider)?.Setting?.Create(); + // var parent = reg.GetParent(); + // Regex regex1 = new Regex(@"^(.+)(?<=\\)(?.*)", RegexOptions.Compiled); + // var match = regex1.Match(reg.Name); + // if (match.Success) + // { + // parent.DeleteSubKeyTree(match.Groups["path"].Value); + // } + //} } public class RegSetting @@ -66,21 +106,31 @@ public class RegSetting public RegistryHive Hive { set; get; } public RegistryView View { set; get; } - - public static implicit operator RegistryKey(RegSetting data) + public RegistryKey Create(bool write=false) { - RegistryKey reg_base = RegistryKey.OpenBaseKey(data.Hive, RegistryView.Registry64); - if (string.IsNullOrEmpty(data.SubKey) == false) + RegistryKey reg_base = RegistryKey.OpenBaseKey(this.Hive, this.View); + if (string.IsNullOrEmpty(this.SubKey) == false) { - RegistryKey reg = reg_base.OpenSubKey(data.SubKey); + RegistryKey reg = reg_base.OpenSubKey(this.SubKey, write); reg_base.Dispose(); return reg; } return reg_base; } + //public static implicit operator RegistryKey(RegSetting data) + //{ + // RegistryKey reg_base = RegistryKey.OpenBaseKey(data.Hive, RegistryView.Registry64); + // if (string.IsNullOrEmpty(data.SubKey) == false) + // { + // RegistryKey reg = reg_base.OpenSubKey(data.SubKey); + // reg_base.Dispose(); + // return reg; + // } + // return reg_base; + //} } - public class EqualityComparerAll : IEqualityComparer + public class EqualityComparerForce : IEqualityComparer { public bool Equals(T x, T y) { @@ -102,36 +152,6 @@ public bool Equals(T x, T y) public int GetHashCode(T obj) { return 0; - //Check whether the object is null - if (Object.ReferenceEquals(obj, null)) return 0; - List hashcodes = new List(); - var pps = typeof(T).GetProperties().Where(p => p.CanRead == true); - foreach (var pp in pps) - { - var typecode = Type.GetTypeCode(pp.PropertyType); - - var ss1 = pp.GetValue(obj, null); - if(ss1 == null) - { - hashcodes.Add(0); - } - else - { - hashcodes.Add(ss1.GetHashCode()); - } - } - - int hashcode = hashcodes.Aggregate((x, y) => x ^ y); - System.Diagnostics.Trace.WriteLine($"hashcode:{hashcode}"); - return hashcode; - //Get hash code for the Name field if it is not null. - //int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode(); - - ////Get hash code for the Code field. - //int hashProductCode = product.Code.GetHashCode(); - - ////Calculate the hash code for the product. - //return hashProductName ^ hashProductCode; } } } diff --git a/QSoft.Reg/RegQueryEx.cs b/QSoft.Reg/RegQueryEx.cs index 9b54a32..2718ee2 100644 --- a/QSoft.Reg/RegQueryEx.cs +++ b/QSoft.Reg/RegQueryEx.cs @@ -59,56 +59,56 @@ public static int Update(this IEnumerable source, Fun return source.Count(); } - public static int RemoveAll(this IQueryable source, Expression> predicate) - { - var removeall = typeof(RegQueryEx).GetMethods().FirstOrDefault(x => - { - Type[] types = new Type[] { typeof(IEnumerable).GetGenericTypeDefinition(), typeof(Func).GetGenericTypeDefinition() }; - bool result = false; - if (x.Name == "RemoveAll" && x.IsGenericMethod == true) - { - var pps = x.GetParameters().Select(y => y.ParameterType.GetGenericTypeDefinition()); - result = pps.Zip(types, (a, b) => new { a, b }).All(c => c.a == c.b); - } - return result; - }); - var methdodcall = Expression.Call(removeall.MakeGenericMethod(typeof(TSource)), source.Expression, predicate); - return source.Provider.Execute(methdodcall); - } - - public static int RemoveAll(this IEnumerable source, Func predicate) - { - Regex regex1 = new Regex(@"^(.+)(?<=\\)(?.*)", RegexOptions.Compiled); - var regs = source as IEnumerable; - if (regs == null) - { - throw new Exception("Source must be RegistryKey"); - } - int count = 0; - var parent = regs.FirstOrDefault()?.GetParent(); - if (parent != null) - { - var zip = source.Zip(regs, (src, reg) => new { src, reg }); - foreach (var oo in zip) - { - if (predicate(oo.src) == true) - { - var match = regex1.Match(oo.reg.Name); - if (match.Success) - { - parent.DeleteSubKeyTree(match.Groups["path"].Value); - count++; - } - } - - - } - parent.Close(); - parent.Dispose(); - } - - return count; - } + //public static int RemoveAll(this IQueryable source, Expression> predicate) + //{ + // var removeall = typeof(RegQueryEx).GetMethods().FirstOrDefault(x => + // { + // Type[] types = new Type[] { typeof(IEnumerable).GetGenericTypeDefinition(), typeof(Func).GetGenericTypeDefinition() }; + // bool result = false; + // if (x.Name == "RemoveAll" && x.IsGenericMethod == true) + // { + // var pps = x.GetParameters().Select(y => y.ParameterType.GetGenericTypeDefinition()); + // result = pps.Zip(types, (a, b) => new { a, b }).All(c => c.a == c.b); + // } + // return result; + // }); + // var methdodcall = Expression.Call(removeall.MakeGenericMethod(typeof(TSource)), source.Expression, predicate); + // return source.Provider.Execute(methdodcall); + //} + + //public static int RemoveAll(this IEnumerable source, Func predicate) + //{ + // Regex regex1 = new Regex(@"^(.+)(?<=\\)(?.*)", RegexOptions.Compiled); + // var regs = source as IEnumerable; + // if (regs == null) + // { + // throw new Exception("Source must be RegistryKey"); + // } + // int count = 0; + // var parent = regs.FirstOrDefault()?.GetParent(); + // if (parent != null) + // { + // var zip = source.Zip(regs, (src, reg) => new { src, reg }); + // foreach (var oo in zip) + // { + // if (predicate(oo.src) == true) + // { + // var match = regex1.Match(oo.reg.Name); + // if (match.Success) + // { + // parent.DeleteSubKeyTree(match.Groups["path"].Value); + // count++; + // } + // } + + + // } + // parent.Close(); + // parent.Dispose(); + // } + + // return count; + //} public static int RemoveAll(this IQueryable source) @@ -130,53 +130,6 @@ public static int RemoveAll(this IQueryable source) return source.Provider.Execute(methdodcall); } - public static IQueryable Except_RegistryKey(this IQueryable source1, IEnumerable source2) - { - if (typeof(TSource2) == typeof(RegistryKey)) - { - var src_regs_2 = source2 as IQueryable; - var groupby = source1.GroupBy(x => x.Name); - var except = source1.Select(x => x.Name).Except(src_regs_2.Select(x => x.Name)); - var ss = source1.Join(except, x => x.Name, y => y, (x, y) => x); - return ss; - } - else - { - var func = source1.First().ToFunc(); - var aa = source1.ToDictionary(x => func(x), x => x); - var compare = aa.Keys.Except(source2, new EqualityComparerAll()); - var dst = compare.Select(x => aa[x]).AsQueryable(); - return dst; - - } - } - - public static IQueryable Except_RegistryKey(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) - { - var src_regs_1 = source1 as IQueryable; - if (src_regs_1 == null) - { - throw new Exception("Source must be RegistryKey"); - } - - if (typeof(TSource2) == typeof(RegistryKey)) - { - var src_regs_2 = source2 as IQueryable; - var except = src_regs_1.Select(x => x.Name).Except(src_regs_2.Select(x => x.Name)); - var ss = src_regs_1.Join(except, x => x.Name, y => y, (x, y) => x); - return ss; - } - else - { - var func = src_regs_1.First().ToFunc(); - var aa = src_regs_1.ToDictionary(x => func(x), x => x); - var compare = aa.Keys.Except(source2, comparer); - var dst = compare.Select(x => aa[x]).AsQueryable(); - return dst; - } - - } - public static int RemoveAll(this IEnumerable source) { Regex regex1 = new Regex(@"^(.+)(?<=\\)(?.*)", RegexOptions.Compiled); @@ -206,6 +159,90 @@ public static int RemoveAll(this IEnumerable source) return count; } + //public static IQueryable Intersect_RegistryKey(this IQueryable source1, IEnumerable source2) + //{ + // if (typeof(TSource2) == typeof(RegistryKey)) + // { + // var src_regs_2 = source2 as IQueryable; + // var groupby = source1.GroupBy(x => x.Name); + // var except = source1.Select(x => x.Name).Intersect(src_regs_2.Select(x => x.Name)); + // var ss = source1.Join(except, x => x.Name, y => y, (x, y) => x); + // return ss; + // } + // else + // { + // var func = source1.First().ToFunc(); + // var aa = source1.ToDictionary(x => func(x), x => x); + // var compare = aa.Keys.Intersect(source2, new EqualityComparerForce()); + // var dst = compare.Select(x => aa[x]).AsQueryable(); + // return dst; + + // } + //} + + //public static IQueryable Intersect_RegistryKey(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) + //{ + // if (typeof(TSource2) == typeof(RegistryKey)) + // { + // var src_regs_2 = source2 as IQueryable; + // var except = source1.Select(x => x.Name).Intersect(src_regs_2.Select(x => x.Name)); + // var ss = source1.Join(except, x => x.Name, y => y, (x, y) => x); + // return ss; + // } + // else + // { + // var func = source1.First().ToFunc(); + // var aa = source1.ToDictionary(x => func(x), x => x); + // var compare = aa.Keys.Intersect(source2, comparer); + // var dst = compare.Select(x => aa[x]).AsQueryable(); + // return dst; + // } + + //} + + //public static IQueryable Except_RegistryKey(this IQueryable source1, IEnumerable source2) + //{ + // if (typeof(TSource2) == typeof(RegistryKey)) + // { + // var src_regs_2 = source2 as IQueryable; + // var groupby = source1.GroupBy(x => x.Name); + // var except = source1.Select(x => x.Name).Except(src_regs_2.Select(x => x.Name)); + // var ss = source1.Join(except, x => x.Name, y => y, (x, y) => x); + // return ss; + // } + // else + // { + // var func = source1.First().ToFunc(); + // var aa = source1.ToDictionary(x => func(x), x => x); + // var compare = aa.Keys.Except(source2, new EqualityComparerForce()); + // var dst = compare.Select(x => aa[x]).AsQueryable(); + // return dst; + + // } + //} + + //public static IQueryable Except_RegistryKey(this IQueryable source1, IEnumerable source2, IEqualityComparer comparer) + //{ + // if (typeof(TSource2) == typeof(RegistryKey)) + // { + // var src_regs_2 = source2 as IQueryable; + // var except = source1.Select(x => x.Name).Except(src_regs_2.Select(x => x.Name)); + // var ss = source1.Join(except, x => x.Name, y => y, (x, y) => x); + // return ss; + // } + // else + // { + // var func = source1.First().ToFunc(); + // var aa = source1.ToDictionary(x => func(x), x => x); + // var compare = aa.Keys.Except(source2, comparer); + // var dst = compare.Select(x => aa[x]).AsQueryable(); + // return dst; + // } + + //} + + + } diff --git a/QSoft.Reg/RegQueryHelper.cs b/QSoft.Reg/RegQueryHelper.cs index bf8c34b..0bf7c24 100644 --- a/QSoft.Reg/RegQueryHelper.cs +++ b/QSoft.Reg/RegQueryHelper.cs @@ -114,20 +114,16 @@ static public Expression ToData(this Type datatype, ParameterExpression param) Expression name = null; if (regattr != null && regattr is RegSubKeyName) { - if (pp.PropertyType.IsGenericTypeDefinition == true && pp.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + RegSubKeyName subkeyname = regattr as RegSubKeyName; + Expression mm = null; + mm = Expression.Property(param, "Name"); + if (subkeyname.IsFullName == false) { - var method = Expression.Property(param, "Name"); - UnaryExpression unary1 = Expression.Convert(method, pp.PropertyType); - var binding = Expression.Bind(pp, unary1); - bindings.Add(binding); - } - else - { - var method = Expression.Property(param, "Name"); - var binding = Expression.Bind(pp, method); - bindings.Add(binding); - + var method = typeof(RegQueryHelper).GetMethod("GetLastSegement"); + mm = Expression.Call(method, mm); } + var binding = Expression.Bind(pp, mm); + bindings.Add(binding); } else if (regattr != null && regattr is RegPropertyName) { @@ -257,12 +253,33 @@ public static Type[] GetTypes(this IEnumerable src, MethodInfo metho return types.Take(args.Length).ToArray(); } + + public static string GetLastSegement(this string src) + { + string basekeyname = ""; + string path = ""; + Regex regex1 = new Regex(@"^(?\w+)[\\](?.+)[\\]", RegexOptions.Compiled); + //Regex regex2 = new Regex(@"^(?\w+)[\\]", RegexOptions.Compiled); + var match = regex1.Match(src); + if (match.Success == true) + { + basekeyname = match.Groups["base"].Value; + path = match.Groups["path"].Value; + } + else + { + basekeyname = src; + } + var segement = src.Replace(basekeyname, "").Replace(path, "").TrimStart('\\'); + return segement; + } + static public RegistryKey GetParent(this RegistryKey src) { string basekeyname = ""; string path = ""; Regex regex1 = new Regex(@"^(?\w+)[\\](?.+)[\\]", RegexOptions.Compiled); - Regex regex2 = new Regex(@"^(?\w+)[\\]", RegexOptions.Compiled); + //Regex regex2 = new Regex(@"^(?\w+)[\\]", RegexOptions.Compiled); var match = regex1.Match(src.Name); if (match.Success == true) { diff --git a/QSoft.Registry.nuspec b/QSoft.Registry.nuspec index 991af0b..1064ac0 100644 --- a/QSoft.Registry.nuspec +++ b/QSoft.Registry.nuspec @@ -2,7 +2,7 @@ QSoft.Registry - 1.0.0.4 + 1.0.0.5 Registry.NET BEN_HSU BEN_HSU @@ -14,13 +14,14 @@ Linq to RegistryKey Copyright © Ben Hsu 2021 + RegistryKey, Linq - 1. Add RegIgnore attribute -2. Add RegPropertyName + 1. Add RegSubKeyName attribute +2. Add RemoveAll function 3. Fix parse Expression issue 4. Fix data type translate issue diff --git a/README.md b/README.md index a869964..bfd3a0f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + + # Use Linq style read registry(Linq to Registry) ## Use Queryable read data * Support Queryable function @@ -9,18 +11,20 @@ Create definition public class InstalledApp { public string DisplayName { set; get; } - public string DisplayVersion { set; get; } + public Version DisplayVersion { set; get; } public int? EstimatedSize { set; get; } } ``` if you want to use exist class, can add attribute,like below code ```csharp -public class App +public class InstalledApp { - [RegPropertyName(Name = "DisplayName")] - public string Name { set; get; } + [RegSubKeyName] + public string Key { set; get; } + public string DisplayName { set; get; } [RegPropertyName(Name = "DisplayVersion")] - public string Version { set; get; } + public Version Version { set; get; } + public int? EstimatedSize { set; get; } [RegIgnore] public int Size { set; get; } } @@ -30,12 +34,12 @@ public class App Create Query ```csharp var regt = new RegQuery() - .useSetting(x => - { - x.Hive = RegistryHive.LocalMachine; - x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; - x.View = RegistryView.Registry64; - }); + .useSetting(x => + { + x.Hive = RegistryHive.LocalMachine; + x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; + x.View = RegistryView.Registry64; + }); ``` Get data ```csharp @@ -44,7 +48,6 @@ var first2 = regt.First(x => x.DisplayName != ""); var last1 = regt.Last(); var last2 = regt.Last(x => x.DisplayName != ""); - var count1 = regt.Count(); var count2 = regt.Count(x => x.DisplayName == "AA"); var all = regt.All(x => x.DisplayName != ""); @@ -63,10 +66,10 @@ var dictonary = regt.ToDictionary(x => x.EstimatedSize); Query data ```csharp var take = regt.Take(10); -var takewhile = regt.TakeWhile(x => x.DisplayVersion <= new Version(2,2,2,2)); -var orderbydesc = regt.OrderByDescending(x => x.DisplayVersion); +var takewhile = regt.TakeWhile(x => x.Version <= new Version(2,2,2,2)); +var orderbydesc = regt.OrderByDescending(x => x.Version); var oderby = regt.OrderBy(x => x.EstimatedSize); -var where = regt.Where(x => x.DisplayName != "" && x.DisplayVersion > new Version(3,3,3,3)); +var where = regt.Where(x => x.DisplayName != "" && x.Version > new Version(3,3,3,3)); ``` Update Data ```csharp @@ -77,6 +80,13 @@ int update_count2 = regt.Update(x => new { DisplayName = $"{x.DisplayName}_AA" } int update_count3 = regt.Where(x=>x.Version>new Version(1,1,1,1)).Update(x => new InstalledApp() { DisplayName = $"{x.DisplayName}_AA" }); int update_count4 = regt.Where(x=>x.Version>new Version(1,1,1,1)).Update(x => new { DisplayName = $"{x.DisplayName}_AA" }); ``` +Remove Data +```csharp +//remove all data +int remove_count1 = regt.RemoveAll(); +//remove where result +int remove_count2 = regt.Where(x=>x.Version==new Version(1,1,1,1)).RemoveAll(); +``` GroupBy Data ```csharp var group1 = regt.GroupBy(x => x.DisplayName); @@ -85,8 +95,8 @@ var group2 = regt.GroupBy(x => x.DisplayName, (key, app) => new { key, app }); - -## Use extension linq function like below code +__Plan to not maintain the extension method in the next version__ +### Use extension linq function like below code ```csharp using QSoft.Registry; using QSoft.Registry.Linq; diff --git a/UnitTestProject1/UnitTest1.cs b/UnitTestProject1/UnitTest1.cs index ae5fc38..dc83f9e 100644 --- a/UnitTestProject1/UnitTest1.cs +++ b/UnitTestProject1/UnitTest1.cs @@ -27,6 +27,8 @@ public int GetHashCode(InstalledApp obj) public class InstalledApp { + [RegSubKeyName] + public string Key { set; get; } public int? Index { set; get; } public string DisplayName { set; get; } [RegPropertyName(Name = "DisplayVersion")] @@ -68,12 +70,12 @@ public LinqToRegistry() List InstallApp_org() { List datas = new List(); - datas.Add(new InstalledApp() { DisplayName = "AA", Version = new Version("1.1.1.1"), EstimatedSize = 10, IsOfficial = true, Index = 0 }); - datas.Add(new InstalledApp() { DisplayName = "BB", Version = new Version("2.2.2.2"), EstimatedSize = 20, IsOfficial = false, Index = 1 }); - datas.Add(new InstalledApp() { DisplayName = "CC", Version = new Version("3.3.3.3"), EstimatedSize = 30, IsOfficial = true, Index = 2 }); - datas.Add(new InstalledApp() { DisplayName = "DD", Version = new Version("4.4.4.4"), EstimatedSize = 40, IsOfficial = false, Index = 3 }); - datas.Add(new InstalledApp() { DisplayName = "EE", Version = new Version("5.5.5.5"), EstimatedSize = 50, IsOfficial = true, Index = 4 }); - datas.Add(new InstalledApp() { DisplayName = "FF", Version = new Version("6.6.6.6"), EstimatedSize = 60, IsOfficial = false, Index = 5 }); + datas.Add(new InstalledApp() { Key="AA", DisplayName = "AA", Version = new Version("1.1.1.1"), EstimatedSize = 10, IsOfficial = true, Index = 0 }); + datas.Add(new InstalledApp() { Key = "BB", DisplayName = "BB", Version = new Version("2.2.2.2"), EstimatedSize = 20, IsOfficial = false, Index = 1 }); + datas.Add(new InstalledApp() { Key = "CC", DisplayName = "CC", Version = new Version("3.3.3.3"), EstimatedSize = 30, IsOfficial = true, Index = 2 }); + datas.Add(new InstalledApp() { Key = "DD", DisplayName = "DD", Version = new Version("4.4.4.4"), EstimatedSize = 40, IsOfficial = false, Index = 3 }); + datas.Add(new InstalledApp() { Key = "EE", DisplayName = "EE", Version = new Version("5.5.5.5"), EstimatedSize = 50, IsOfficial = true, Index = 4 }); + datas.Add(new InstalledApp() { Key = "FF", DisplayName = "FF", Version = new Version("6.6.6.6"), EstimatedSize = 60, IsOfficial = false, Index = 5 }); return datas; } @@ -106,7 +108,7 @@ public void BuildMockup() var test1A = reg.CreateSubKey(@"1A", true); foreach(var oo in this.m_Tests) { - var regd = test1A.CreateSubKey(oo.DisplayName, true); + var regd = test1A.CreateSubKey(oo.Key, true); foreach(var pp in propertys) { var doo = pp.GetValue(oo); @@ -197,9 +199,10 @@ public void Take() [TestMethod] public void TakeWhile() { - this.Check(this.m_Tests.TakeWhile(x=>x.DisplayName=="AA"), regt.TakeWhile(x => x.DisplayName == "AA")); + this.Check(this.m_Tests.TakeWhile(x => x.DisplayName == "AA"), regt.TakeWhile(x => x.DisplayName == "AA")); this.Check(this.m_Tests.TakeWhile(x => x.EstimatedSize.ToString() == "10"), regt.TakeWhile(x => x.EstimatedSize.ToString() == "10")); this.Check(this.m_Tests.TakeWhile(x => x.DisplayName == "AA".ToString()), regt.TakeWhile(x => x.DisplayName == "AA".ToString())); + this.Check(this.m_Tests.TakeWhile(x => x.Key.Contains("AA".ToString())), regt.TakeWhile(x => x.Key.Contains("AA".ToString()))); } [TestMethod] @@ -438,6 +441,7 @@ public void Any() [TestMethod] public void All() { + Assert.IsTrue(this.m_Tests.All(x => x.DisplayName == "") == regt.All(x => x.DisplayName == ""), "All(x => x.DisplayName == ) fail"); Assert.IsTrue(this.m_Tests.All(x => x.DisplayName == "" && x.EstimatedSize > 10) == regt.All(x => x.DisplayName == "" && x.EstimatedSize > 10), "All fail"); } @@ -448,6 +452,7 @@ public void Count() Assert.IsTrue(this.m_Tests.Count() == regt.Count(), "Count fail"); Assert.IsTrue(this.m_Tests.Count(x=>x.EstimatedSize>30) == regt.Count(x => x.EstimatedSize > 30), "Count fail"); Assert.IsTrue(this.m_Tests.Count(x=>x.EstimatedSize>=40&&x.DisplayName=="") == regt.Count(x => x.EstimatedSize >= 40 && x.DisplayName == ""), "Count fail"); + Assert.IsTrue(this.m_Tests.Count(x=>x.Key.Length>=2) == regt.Count(x => x.Key.Length >= 2), "Count fail"); } [TestMethod]