Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
oven425 committed Dec 9, 2021
1 parent edf8301 commit 73a3298
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 59 deletions.
44 changes: 35 additions & 9 deletions ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Win32;
using QSoft.Registry;
Expand Down Expand Up @@ -88,7 +89,30 @@ static void Main(string[] args)
installs.Add(new InstalledApp() { DisplayName = "A", IsOfficial = true });
installs.Add(new InstalledApp() { DisplayName = "AA", IsOfficial = false });
#if Queryable
Dictionary<int, Dictionary<string, Type>> uu = new Dictionary<int, Dictionary<string, Type>>();
uu[0] = new Dictionary<string, Type>();
uu[0]["1"] = typeof(int);
uu[1] = uu[0];



//string full = @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
//var sss = full.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
//var ss1 = sss.Skip(1).Take(sss.Length - 2);

//var ssssss = ss1.Aggregate((x, y) => $"{x}\\{y}");


//RegistryKey regbase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
//var reg1 = regbase.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", true);
//var parent = regbase.GetParent();
//Regex regex = new Regex(@"[\\]([A-Za-z0-9]+)$");
//Regex regex = new Regex(@"^(?<base>\w+)[\\]");


var queryreg = RegistryHive.LocalMachine.OpenView64(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A", false);


List<RegistryKey> ll = new List<RegistryKey>();
foreach (var subkeyname in queryreg.GetSubKeyNames())
{
Expand Down Expand Up @@ -122,7 +146,9 @@ static void Main(string[] args)
x.SubKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\1A";
x.View = RegistryView.Registry64;
});

int update_count = regt.Update(x => new InstalledApp() { EstimatedSize = x.EstimatedSize + 100 });
var avg = regt.Average(x => x.Version.ToString().Length);
//var group4 = regt.GroupBy(x => x.DisplayName, x => x.EstimatedSize, (key, data) => data.Count());
List<int> src1 = new List<int> { 1, 2, 3, 7, 7 };
List<int> src2 = new List<int>() { 3, 4, 5 };
//var dst1 = src1.Except(src2);
Expand Down Expand Up @@ -173,16 +199,16 @@ static void Main(string[] args)
var group7 = regt.GroupBy(x => x.DisplayName, y=>y.EstimatedSize, (x,e)=>new { x,e});//1006

//var fi = regt.FirstOrDefault();
try
{
int update_count2 = regt.Where(x=>x.IsOfficial==true).Update(x => new { EstimatedSize = x.EstimatedSize + 100 });
}
catch(Exception ee)
{
//try
//{
// int update_count2 = regt.Where(x=>x.IsOfficial==true).Update(x => new { EstimatedSize = x.EstimatedSize + 100 });
//}
//catch(Exception ee)
//{

}
//}

int update_count = regt.Where(x=>x.Version>new Version(1,1,1,1)).Update(x => new InstalledApp() { DisplayName = $"{x.DisplayName}_AA" });
int update_count12 = regt.Where(x => x.EstimatedSize > 130).Update(x => new InstalledApp() { EstimatedSize = x.EstimatedSize - 100 });
//var group2 = regt.GroupBy(x => x.DisplayName, (key, app) => new { key, app });
var takewhile1 = regt.Where(x =>x.IsOfficial==false&& x.Version > new Version(3,3,3,3));
var ex = takewhile1 as Exception;
Expand Down
2 changes: 1 addition & 1 deletion QSoft.Reg/QSoft.Registry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>QSoft.Registry</RootNamespace>
<AssemblyName>QSoft.Registry</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
Expand Down
48 changes: 37 additions & 11 deletions QSoft.Reg/RegExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public Expression Visit(Expression node, Type datatype, Expression regfunc)
public Expression Visit(Type datatype, MethodInfo method, params Expression[] nodes)
{
System.Diagnostics.Debug.WriteLine($"Visit {method?.Name}");

this.PreparMethod(nodes[1], new Expression[] { nodes[0], nodes[1] }, method);
this.m_DataType = datatype;
this.PreparMethod(nodes[1], new Expression[] { nodes[0], nodes[1] }, method);


Expression expr = this.Visit(nodes[1]);

Expand Down Expand Up @@ -254,14 +254,14 @@ protected override Expression VisitParameter(ParameterExpression node)
{
if(pp1.IsGenericType == true&&pp1.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
var type = this.m_GenericTypes[pp1.GetGenericArguments()[0].Name];
var type = this.m_GenericTypes.First()[pp1.GetGenericArguments()[0].Name];
var ie = pp1.GetGenericTypeDefinition().MakeGenericType(type);
var pp = Expression.Parameter(ie, expr.Name);
this.m_Parameters[expr.Name] = pp;
}
else if(this.m_GenericTypes.ContainsKey(pp1.Name) == true)
else if(this.m_GenericTypes.First().ContainsKey(pp1.Name) == true)
{
var type = this.m_GenericTypes[pp1.Name];
var type = this.m_GenericTypes.First()[pp1.Name];
var pp = Expression.Parameter(type, expr.Name);
this.m_Parameters[expr.Name] = pp;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ protected override Expression VisitMember(MemberExpression node)
}

Tuple<ParameterInfo, MethodInfo, string> m_PP = null;
Dictionary<string, Type> m_GenericTypes = new Dictionary<string, Type>();
Stack<Dictionary<string, Type>> m_GenericTypes = new Stack<Dictionary<string, Type>>();
protected override Expression VisitUnary(UnaryExpression node)
{
m_PP = null;
Expand Down Expand Up @@ -372,7 +372,7 @@ protected override Expression VisitUnary(UnaryExpression node)
var lambda = exprs.First().Value as LambdaExpression;
if (lambda != null)
{
this.m_GenericTypes[kkey] = lambda.ReturnType;
this.m_GenericTypes.First()[kkey] = lambda.ReturnType;
this.m_Parameters.Clear();
}
}
Expand All @@ -398,26 +398,46 @@ void PreparMethod(Expression node, Expression[] args, MethodInfo method)
{
m_ExpressionSaves[node] = null;
System.Diagnostics.Debug.WriteLine($"PreparMethod {method?.Name}");

if (method.IsGenericMethod == true)
{
this.m_GenericTypes.Push(new Dictionary<string, Type>());
var dic = method.GetParameters();
var dicc = method.GetGenericMethodDefinition().GetGenericArguments();
for (int i = 0; i < dicc.Length; i++)
{
if (i == 0)
{
this.m_GenericTypes[dicc[i].Name] = typeof(RegistryKey);
if(dic[i].ParameterType == typeof(IEnumerable<>).GetGenericTypeDefinition().MakeGenericType(this.m_DataType))
{
this.m_GenericTypes.First()[dicc[i].Name] = typeof(RegistryKey);
}
else if (dic[i].ParameterType == typeof(IQueryable<>).GetGenericTypeDefinition().MakeGenericType(this.m_DataType))
{
this.m_GenericTypes.First()[dicc[i].Name] = typeof(RegistryKey);
}
else
{
if(dic[i].ParameterType.IsGenericType == true)
{
this.m_GenericTypes.First()[dicc[i].Name] = dic[i].ParameterType.GetGenericArguments()[0];
}
else
{
this.m_GenericTypes.First()[dicc[i].Name] = dic[i].ParameterType;
}
}

}
else
{
if (dic[i].ParameterType.IsGenericType == true && dic[i].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
this.m_GenericTypes[dicc[i].Name] = dic[i].ParameterType.GetGenericArguments()[0];
this.m_GenericTypes.First()[dicc[i].Name] = dic[i].ParameterType.GetGenericArguments()[0];
}
else
{
this.m_GenericTypes[dicc[i].Name] = null;
this.m_GenericTypes.First()[dicc[i].Name] = null;
}
}
}
Expand Down Expand Up @@ -518,6 +538,12 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
}

}
if(expr.Method.IsGenericMethod == true && this.m_GenericTypes.Count > 0)
{
this.m_GenericTypes.Pop();
}


this.m_ExpressionSaves[expr] = methodcall;
this.m_Lastnode = expr;
return expr;
Expand Down
3 changes: 2 additions & 1 deletion QSoft.Reg/RegProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
if (method1.Arguments[0].NodeType == ExpressionType.Constant)
{
this.m_Errors.Clear();
this.m_RegMethod = reg.Visit(expression, typeof(TElement), this.m_RegSource);
//this.m_RegMethod = reg.Visit(expression, typeof(TElement), this.m_RegSource);
this.m_RegMethod = reg.Visit(expression, this.m_DataType, this.m_RegSource);
if (reg.Fail != null)
{
this.m_Errors.Add(Tuple.Create(expression, this.m_RegMethod, reg.Fail));
Expand Down
68 changes: 68 additions & 0 deletions QSoft.Reg/RegQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace QSoft.Registry.Linq
{
Expand Down Expand Up @@ -344,6 +345,73 @@ public static Type[] GetTypes(this IEnumerable<Expression> src, MethodInfo metho

return types.Take(args.Length).ToArray();
}

static public RegistryKey GetParent(this RegistryKey src)
{
string basekeyname = "";
string path = "";
Regex regex1 = new Regex(@"^(?<base>\w+)[\\](?<path>.+)[\\]", RegexOptions.Compiled);
Regex regex2 = new Regex(@"^(?<base>\w+)[\\]", RegexOptions.Compiled);
var match = regex1.Match(src.Name);
if (match.Success == true)
{
basekeyname = match.Groups["base"].Value;
path = match.Groups["path"].Value;
}
else
{
basekeyname = src.Name;
}
RegistryHive hive;
switch (basekeyname)
{
case "HKEY_CURRENT_USER":
{
hive = RegistryHive.CurrentUser;
}
break;
case "HKEY_CLASSES_ROOT":
{
hive = RegistryHive.ClassesRoot;
}
break;
case "HKEY_LOCAL_MACHINE":
{
hive = RegistryHive.LocalMachine;
}
break;
case "HKEY_CURRENT_CONFIG":
{
hive = RegistryHive.CurrentConfig;
}
break;
case "HKEY_DYN_DATA":
{
hive = RegistryHive.DynData;
}
break;
case "HKEY_PERFORMANCE_DATA":
{
hive = RegistryHive.PerformanceData;
}
break;
case "HKEY_USERS":
{
hive = RegistryHive.Users;
}
break;
default:
{
throw new Exception("RegistryHive not find");
}
break;
}
var basekey = RegistryKey.OpenBaseKey(hive,src.View);
var reg = basekey.OpenSubKey(path, true);
basekey.Close();
basekey.Dispose();
return reg;
}
}


Expand Down
8 changes: 5 additions & 3 deletions QSoft.Registry.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>QSoft.Registry</id>
<version>1.0.0.3</version>
<version>1.0.0.4</version>
<title>Registry.NET</title>
<authors>BEN_HSU</authors>
<owners>BEN_HSU</owners>
Expand All @@ -19,8 +19,10 @@
<group targetFramework=".NETFramework4.7.2" />
<group targetFramework=".NETFramework4.8" />
</dependencies>
<releaseNotes>1. Fix parse Expression issue
2. Fix data type translate issue</releaseNotes>
<releaseNotes>1. Add RegIgnore attribute
2. Add RegPropertyName
3. Fix parse Expression issue
4. Fix data type translate issue</releaseNotes>
</metadata>
<files>
<file src="QSoft.Reg\bin\Release\lib\" target="lib" />
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class App
Create Query
```csharp
var regt = new RegQuery<InstalledApp>()
.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
Expand Down
Loading

0 comments on commit 73a3298

Please sign in to comment.