Add project files.
This commit is contained in:
parent
d292bae793
commit
7d293db963
34
ProgrammList.sln
Normal file
34
ProgrammList.sln
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgrammList", "ProgrammList\ProgrammList.csproj", "{06D431CD-A938-4DFD-8D77-4C612FDAD914}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
PrgmList|Any CPU = PrgmList|Any CPU
|
||||
PrgmList|x64 = PrgmList|x64
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|Any CPU.Build.0 = Release|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Debug|x64.Build.0 = Debug|x64
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.PrgmList|Any CPU.ActiveCfg = PrgmList|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.PrgmList|Any CPU.Build.0 = PrgmList|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.PrgmList|x64.ActiveCfg = PrgmList|x64
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.PrgmList|x64.Build.0 = PrgmList|x64
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Release|x64.ActiveCfg = Release|x64
|
||||
{06D431CD-A938-4DFD-8D77-4C612FDAD914}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
18
ProgrammList/Program.cs
Normal file
18
ProgrammList/Program.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProgrammList.ListPrograms;
|
||||
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
|
||||
ListPrograms list = new ListPrograms();
|
||||
|
||||
Console.WriteLine("Deleting old data");
|
||||
list.DeleteOldData();
|
||||
string keyname1 = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstal";
|
||||
Console.WriteLine("Searching for 32 bit");
|
||||
list.createList(keyname1, "x86");
|
||||
string keyname2 = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
|
||||
Console.WriteLine("Searching for 64 bit");
|
||||
list.createList(keyname2, "x64");
|
||||
}
|
||||
}
|
20
ProgrammList/ProgrammList.csproj
Normal file
20
ProgrammList/ProgrammList.csproj
Normal file
@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<Configurations>Debug;Release;PrgmList</Configurations>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
|
||||
<PackageReference Include="MySql.Data" Version="9.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
70
ProgrammList/list_creation/ListPrograms.cs
Normal file
70
ProgrammList/list_creation/ListPrograms.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Win32;
|
||||
using ProgrammList.sql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Mysqlx.Expect.Open.Types.Condition.Types;
|
||||
|
||||
namespace ProgrammList.ListPrograms {
|
||||
internal class ListPrograms {
|
||||
|
||||
string prgm_path = Directory.GetCurrentDirectory() + "\\";
|
||||
string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate"};
|
||||
Mysql sql;
|
||||
|
||||
internal ListPrograms() {
|
||||
sql = new Mysql();
|
||||
}
|
||||
|
||||
internal void DeleteOldData() {
|
||||
sql.checkTableExists();
|
||||
sql.deleteOldData(Dns.GetHostName());
|
||||
}
|
||||
|
||||
internal void createList(string hkey, string bit) {
|
||||
RegistryKey key = Registry.LocalMachine.OpenSubKey(hkey);
|
||||
try {
|
||||
if (key == null) {
|
||||
return;
|
||||
}
|
||||
string[] programmIds = key.GetSubKeyNames();
|
||||
|
||||
|
||||
foreach (string programId in programmIds) {
|
||||
|
||||
RegistryKey programIdSubIds = key.OpenSubKey(programId);
|
||||
|
||||
string result = "";
|
||||
int count = 0;
|
||||
Dictionary<string, String> value = new Dictionary<string, string>();
|
||||
for (var i = 0; i <= keyvaluenames.Length - 1; i++) {
|
||||
value.Add(keyvaluenames[i], "'" + (string)programIdSubIds.GetValue(keyvaluenames[i]) + "'");
|
||||
count++;
|
||||
}
|
||||
|
||||
result = String.Join("", value.ToArray());
|
||||
|
||||
if (result.EndsWith(",")) {
|
||||
result = result.Remove(result.Length - 1);
|
||||
}
|
||||
|
||||
value.Add("PCID", "'" + Dns.GetHostName() + "'");
|
||||
value.Add("update_date", "'" + DateTime.Now + "'");
|
||||
value.Add("APP_Architecture", "'" + bit + "'");
|
||||
|
||||
|
||||
sql.InsertOrUpdateData(value);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.Error.WriteLine(e.ToString());
|
||||
System.Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
159
ProgrammList/sql/MySql.cs
Normal file
159
ProgrammList/sql/MySql.cs
Normal file
@ -0,0 +1,159 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace ProgrammList.sql {
|
||||
|
||||
|
||||
public class Mysql {
|
||||
|
||||
string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
||||
|
||||
public MySqlConnection Connection;
|
||||
|
||||
//private static DbConnection instance;
|
||||
public Mysql() {
|
||||
//string CnnStr = "Data Source=local;Initial Catalog=programlist;User Id=prgmlist;pwd=G0KaUM7TzgO7ZoPZCifs";
|
||||
// instance = new MySqlConnection(CnnStr);
|
||||
}
|
||||
|
||||
public void Open() {
|
||||
string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}", "localhost", "programlist", "prgmlist", "G0KaUM7TzgO7ZoPZCifs");
|
||||
Connection = new MySqlConnection(connstring);
|
||||
Connection.Open();
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
Connection.Close();
|
||||
}
|
||||
|
||||
|
||||
internal void getAllData() {
|
||||
Open();
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = @"SELECT * FROM list";
|
||||
|
||||
using (var reader = command.ExecuteReader()) {
|
||||
while (reader.Read()) {
|
||||
var dataLine = reader.GetString(0);
|
||||
}
|
||||
}
|
||||
Close();
|
||||
}
|
||||
internal Boolean getSingleLine(string pcid, string program, string version) {
|
||||
Open();
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = @"SELECT * FROM list where PCID like "
|
||||
+ pcid + " and DisplayName like "
|
||||
+ program + " and DisplayVersion like " + version + ";";
|
||||
var result = command.ExecuteReader().Read();
|
||||
|
||||
Close();
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void checkTableExists() {
|
||||
Open();
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = @"SHOW TABLES LIKE 'list';";
|
||||
var name = command.ExecuteScalar();
|
||||
if (name != null && name.ToString() == "list") {
|
||||
return;
|
||||
}
|
||||
var cols = string.Join(" VARCHAR(255),", valuenames);
|
||||
cols = cols + " Varchar(255)";
|
||||
command.CommandText = "CREATE TABLE list (" + cols + ")";
|
||||
command.ExecuteNonQuery();
|
||||
Close();
|
||||
}
|
||||
|
||||
|
||||
internal void InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||
Open();
|
||||
var transaction = Connection.BeginTransaction();
|
||||
|
||||
string result = "";
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
result += valuesqlCommand.GetValueOrDefault(valuenames[i]);
|
||||
|
||||
if (i < valuenames.Length - 1) {
|
||||
result += ",";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var cols = String.Join(",", valuenames);
|
||||
|
||||
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
||||
|
||||
var command = new MySqlCommand(sqlCommand, Connection, transaction);
|
||||
command.ExecuteNonQuery();
|
||||
transaction.Commit();
|
||||
Console.WriteLine(sqlCommand);
|
||||
Close();
|
||||
}
|
||||
|
||||
internal void InsertOrUpdateData(Dictionary<string, string> value) {
|
||||
if (getSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
||||
UpdateData(value);
|
||||
}
|
||||
else {
|
||||
InsertData(value);
|
||||
}
|
||||
}
|
||||
|
||||
internal void deleteOldData(string hostname) {
|
||||
Open();
|
||||
var command = Connection.CreateCommand();
|
||||
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
||||
command.CommandText = sqlCommand;
|
||||
command.ExecuteReader();
|
||||
Close();
|
||||
}
|
||||
|
||||
internal void UpdateData(Dictionary<string, string> value) {
|
||||
Open();
|
||||
var transaction = Connection.BeginTransaction();
|
||||
string sqlCommand = @"Update list ";
|
||||
|
||||
string result = "set ";
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]);
|
||||
|
||||
if (i < valuenames.Length - 1) {
|
||||
result += " ,";
|
||||
}
|
||||
}
|
||||
|
||||
sqlCommand = sqlCommand + result;
|
||||
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") +
|
||||
" and DisplayName like " + value.GetValueOrDefault("DisplayName") +
|
||||
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
||||
|
||||
|
||||
var command = new MySqlCommand(sqlCommand, Connection, transaction);
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
if (valuenames[i] != "PCID") {
|
||||
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
||||
}
|
||||
}
|
||||
Console.WriteLine(sqlCommand);
|
||||
command.ExecuteNonQuery();
|
||||
transaction.Commit();
|
||||
Close();
|
||||
}
|
||||
|
||||
internal void DeleteData(string id) {
|
||||
Open();
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
||||
command.Parameters.AddWithValue("$id", id);
|
||||
|
||||
using (var reader = command.ExecuteReader()) {
|
||||
while (reader.Read()) {
|
||||
var name = reader.GetString(0);
|
||||
}
|
||||
}
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
162
ProgrammList/sql/SqlBase.cs
Normal file
162
ProgrammList/sql/SqlBase.cs
Normal file
@ -0,0 +1,162 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ProgrammList.sql {
|
||||
|
||||
/**
|
||||
* Basic commands to connect to the Sqlight database
|
||||
*/
|
||||
internal class SqlBase {
|
||||
|
||||
SqliteConnection connection;
|
||||
string prgm_path;
|
||||
string filename;
|
||||
|
||||
string[] valuenames = { "PCID", "DisplayName", "DisplayVersion", "InstallDate", "update_date", "APP_Architecture" };
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
internal SqlBase(string prm_path, string file) {
|
||||
|
||||
prgm_path = prm_path + filename;
|
||||
filename = file;
|
||||
|
||||
//connection = new SqliteConnection("Data Source=" + prm_path + filename + " ;PRAGMA journal_mode=WAL;");
|
||||
connection = new SqliteConnection("Data Source=" + prm_path + filename);
|
||||
connection.Open();
|
||||
}
|
||||
|
||||
internal void getAllData() {
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = @"SELECT * FROM list";
|
||||
|
||||
using (var reader = command.ExecuteReader()) {
|
||||
while (reader.Read()) {
|
||||
var dataLine = reader.GetString(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
internal Boolean getSingleLine(string pcid, string program, string version) {
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = @"SELECT * FROM list where PCID like "
|
||||
+ pcid + " and DisplayName like "
|
||||
+ program + " and DisplayVersion like " + version + ";";
|
||||
|
||||
|
||||
bool result = command.ExecuteReader().Read();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void checkTableExists() {
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = @"SELECT name FROM sqlite_master WHERE type='table' AND name='list';";
|
||||
var name = command.ExecuteScalar();
|
||||
if (name != null && name.ToString() == "list") {
|
||||
return;
|
||||
}
|
||||
var cols = string.Join(" VARCHAR,", valuenames);
|
||||
cols = cols + " Varchar";
|
||||
command.CommandText = "CREATE TABLE list (" +cols + ")";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
|
||||
internal void InsertData(Dictionary<string, string> valuesqlCommand) {
|
||||
var transaction = connection.BeginTransaction();
|
||||
|
||||
string result = "";
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
result += valuesqlCommand.GetValueOrDefault(valuenames[i]);
|
||||
|
||||
if (i < valuenames.Length -1) {
|
||||
result += ",";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var cols = String.Join(",", valuenames);
|
||||
|
||||
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
|
||||
|
||||
var command = new SqliteCommand(sqlCommand, connection, transaction);
|
||||
Console.WriteLine(sqlCommand);
|
||||
command.ExecuteNonQuery();
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
internal void InsertOrUpdateData(Dictionary<string, string> value) {
|
||||
if (getSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) {
|
||||
|
||||
Console.WriteLine("Update");
|
||||
UpdateData(value);
|
||||
}
|
||||
else {
|
||||
|
||||
Console.WriteLine("Insert");
|
||||
InsertData(value);
|
||||
}
|
||||
}
|
||||
|
||||
internal void deleteOldData(string hostname) {
|
||||
var command = connection.CreateCommand();
|
||||
string sqlCommand = @"delete from list where PCID = '" + hostname + "';";
|
||||
command.CommandText = sqlCommand;
|
||||
command.ExecuteReader();
|
||||
}
|
||||
|
||||
internal void UpdateData(Dictionary<string, string> value) {
|
||||
var transaction = connection.BeginTransaction();
|
||||
string sqlCommand = @"Update list ";
|
||||
|
||||
string result = "set ";
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]);
|
||||
|
||||
if (i < valuenames.Length - 1) {
|
||||
result += " ,";
|
||||
}
|
||||
}
|
||||
|
||||
sqlCommand = sqlCommand + result;
|
||||
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") +
|
||||
" and DisplayName like " + value.GetValueOrDefault("DisplayName") +
|
||||
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion");
|
||||
|
||||
|
||||
var command = new SqliteCommand(sqlCommand, connection, transaction);
|
||||
for (int i = 0; i < valuenames.Length; i++) {
|
||||
if (valuenames[i] != "PCID") {
|
||||
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i]));
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(command.CommandText);
|
||||
command.ExecuteNonQuery();
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
internal void DeleteData(string id) {
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = @"SELECT name FROM user WHERE id = $id";
|
||||
command.Parameters.AddWithValue("$id", id);
|
||||
|
||||
using (var reader = command.ExecuteReader()) {
|
||||
while (reader.Read()) {
|
||||
var name = reader.GetString(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user