neue version für .net framework 4.6.2

unterstützung für app.config
This commit is contained in:
Kuro 2025-03-07 21:05:03 +01:00
parent ef7493a09a
commit 6599e17a18
9 changed files with 151 additions and 68 deletions

View File

@ -1,17 +0,0 @@
using System.Configuration;
namespace ProgrammList.ConfigManager {
public class ConfigManager {
public static string GetSetting(string key) {
return ConfigurationManager.AppSettings[key];
}
public static void SetSetting(string key, string value) {
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
}
}
}

View File

@ -1,12 +1,13 @@
using ProgrammList.ConfigManager; using ProgrammList.ConfigManager;
using ProgrammList.ListPrograms; using ProgrammList.ListPrograms;
using System;
class Program { class Program {
public static void Main(string[] args) { public static void Main(string[] args) {
string dbType = ConfigManager.GetSetting("DB_Type"); string dbType = PrmListConfigManager.GetSetting("DB_Type");
ListPrograms list = new ListPrograms(dbType); ListPrograms list = new ListPrograms();
list.init();
Console.WriteLine("Deleting old data"); Console.WriteLine("Deleting old data");
list.DeleteOldData(); list.DeleteOldData();
string keyname1 = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstal"; string keyname1 = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstal";

View File

@ -2,13 +2,16 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <ImplicitUsings>disable</ImplicitUsings>
<ImplicitUsings>enable</ImplicitUsings> <Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
<Configurations>Debug;Release;PrgmList</Configurations> <Configurations>Debug;Release;PrgmList</Configurations>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Content Include="Icons\prm_icon\back-end.ico" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.2" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.2" />
@ -16,5 +19,29 @@
<PackageReference Include="MySql.Data" Version="9.2.0" /> <PackageReference Include="MySql.Data" Version="9.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net462</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<AssemblyName>ProgramList</AssemblyName>
<ApplicationIcon>Icons\prm_icon\back-end.ico</ApplicationIcon>
<StartupObject>Program</StartupObject>
</PropertyGroup>
</Project> </Project>

View File

@ -1,15 +1,20 @@
using Microsoft.Win32; using Microsoft.Win32;
using ProgrammList.ConfigManager;
using ProgrammList.sql; using ProgrammList.sql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
namespace ProgrammList.ListPrograms { namespace ProgrammList.ListPrograms {
internal class ListPrograms { public class ListPrograms {
string prgm_path = Directory.GetCurrentDirectory() + "\\"; string prgm_path = Directory.GetCurrentDirectory() + "\\";
string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate" }; string[] keyvaluenames = { "DisplayName", "DisplayVersion", "InstallDate" };
private SqlBase sql = null; private SqlBase sql = null;
internal ListPrograms(string sqlType) { public void init() {
string sqlType = PrmListConfigManager.GetSetting("DB_Type");
if (sqlType == null) { if (sqlType == null) {
Console.WriteLine("SQL Database not defined in app.conf, allowed types:"); Console.WriteLine("SQL Database not defined in app.conf, allowed types:");
Console.WriteLine("MYSQL"); Console.WriteLine("MYSQL");
@ -30,12 +35,12 @@ namespace ProgrammList.ListPrograms {
} }
} }
internal void DeleteOldData() { public void DeleteOldData() {
sql.CheckTableExists(); sql.CheckTableExists();
sql.DeleteOldData(Dns.GetHostName()); sql.DeleteOldData(Dns.GetHostName());
} }
internal void createList(string hkey, string bit) { public void createList(string hkey, string bit) {
RegistryKey key = Registry.LocalMachine.OpenSubKey(hkey); RegistryKey key = Registry.LocalMachine.OpenSubKey(hkey);
try { try {
if (key == null) { if (key == null) {
@ -56,11 +61,11 @@ namespace ProgrammList.ListPrograms {
count++; count++;
} }
result = String.Join("", value.ToArray()); // result = String.Join("", value.ToArray());
//
if (result.EndsWith(",")) { // if (result.EndsWith(",")) {
result = result.Remove(result.Length - 1); // result = result.Remove(result.Length - 1);
} // }
value.Add("PCID", "'" + Dns.GetHostName() + "'"); value.Add("PCID", "'" + Dns.GetHostName() + "'");
value.Add("update_date", "'" + DateTime.Now + "'"); value.Add("update_date", "'" + DateTime.Now + "'");

View File

@ -1,6 +1,8 @@
 
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
namespace ProgrammList.sql { namespace ProgrammList.sql {
public class Mssql : SqlBaseAbstract { public class Mssql : SqlBaseAbstract {
@ -46,6 +48,8 @@ namespace ProgrammList.sql {
Open(DB.MSSQL); Open(DB.MSSQL);
var command = sqlcon.CreateCommand(); var command = sqlcon.CreateCommand();
command.CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end"; command.CommandText = "select case when exists (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'prgmlist' AND TABLE_NAME = 'list') then 1 else 0 end";
Console.WriteLine("Executing: " + command.CommandText);
var returncode = command.ExecuteScalar(); var returncode = command.ExecuteScalar();
if (returncode != null) { if (returncode != null) {
int rc; int rc;
@ -76,7 +80,9 @@ namespace ProgrammList.sql {
string result = ""; string result = "";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuesqlCommand.GetValueOrDefault(valuenames[i]); string val = "";
valuesqlCommand.TryGetValue(valuenames[i], out val);
result += val;
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += ","; result += ",";
@ -96,7 +102,13 @@ namespace ProgrammList.sql {
} }
public void InsertOrUpdateData(Dictionary<string, string> value) { public void InsertOrUpdateData(Dictionary<string, string> value) {
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) { string pcid = "";
value.TryGetValue("PCID", out pcid);
string displayName = "";
value.TryGetValue("DisplayName", out displayName);
string displayVersion = "";
value.TryGetValue("DisplayVersion", out displayVersion);
if (GetSingleLine(pcid, displayName, displayVersion)) {
UpdateData(value); UpdateData(value);
} }
else { else {
@ -120,7 +132,9 @@ namespace ProgrammList.sql {
string result = "set "; string result = "set ";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]); string val = "";
value.TryGetValue(valuenames[i], out val);
result += valuenames[i] + " = " + val;
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += " ,"; result += " ,";
@ -128,15 +142,23 @@ namespace ProgrammList.sql {
} }
sqlCommand = sqlCommand + result; sqlCommand = sqlCommand + result;
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") + string pcid = "";
" and DisplayName like " + value.GetValueOrDefault("DisplayName") + value.TryGetValue("PCID", out pcid);
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion"); string displayName = "";
value.TryGetValue("DisplayName", out displayName);
string displayVersion = "";
value.TryGetValue("DisplayVersion", out displayVersion);
sqlCommand = sqlCommand + " WHERE PCID = " + pcid +
" and DisplayName like " + displayName +
" and DisplayVersion like " + displayVersion;
var command = new SqlCommand(sqlCommand, sqlcon, transaction); var command = new SqlCommand(sqlCommand, sqlcon, transaction);
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
if (valuenames[i] != "PCID") { if (valuenames[i] != "PCID") {
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i])); string itemValue = "";
value.TryGetValue(valuenames[i], out itemValue);
command.Parameters.AddWithValue("$" + valuenames[i], itemValue);
} }
} }
Console.WriteLine(sqlCommand); Console.WriteLine(sqlCommand);

View File

@ -1,9 +1,11 @@
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
namespace ProgrammList.sql { namespace ProgrammList.sql {
internal class Mysql : SqlBaseAbstract { public class Mysql : SqlBaseAbstract {
public Boolean GetSingleLine(string pcid, string program, string version) { public Boolean GetSingleLine(string pcid, string program, string version) {
@ -40,7 +42,9 @@ namespace ProgrammList.sql {
string result = ""; string result = "";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuesqlCommand.GetValueOrDefault(valuenames[i]); string val = "";
valuesqlCommand.TryGetValue(valuenames[i], out val);
result += val;
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += ","; result += ",";
@ -48,7 +52,7 @@ namespace ProgrammList.sql {
} }
var cols = String.Join(",", valuenames); var cols = string.Join(",", valuenames);
string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")"; string sqlCommand = "INSERT INTO list(" + cols + ")" + "VALUES(" + result + ")";
@ -60,7 +64,13 @@ namespace ProgrammList.sql {
} }
public void InsertOrUpdateData(Dictionary<string, string> value) { public void InsertOrUpdateData(Dictionary<string, string> value) {
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) { string pcid = "";
value.TryGetValue("PCID", out pcid);
string displayName = "";
value.TryGetValue("DisplayName", out displayName);
string displayVersion = "";
value.TryGetValue("DisplayVersion", out displayVersion);
if (GetSingleLine(pcid, displayName, displayVersion)) {
UpdateData(value); UpdateData(value);
} }
else { else {
@ -75,7 +85,9 @@ namespace ProgrammList.sql {
string result = "set "; string result = "set ";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]); string val = "";
value.TryGetValue(valuenames[i], out val);
result += valuenames[i] + " = " + val;
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += " ,"; result += " ,";
@ -83,15 +95,23 @@ namespace ProgrammList.sql {
} }
sqlCommand = sqlCommand + result; sqlCommand = sqlCommand + result;
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") + string pcid = "";
" and DisplayName like " + value.GetValueOrDefault("DisplayName") + value.TryGetValue("PCID", out pcid);
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion"); string displayName = "";
value.TryGetValue("DisplayName", out displayName);
string displayVersion = "";
value.TryGetValue("DisplayVersion", out displayVersion);
sqlCommand = sqlCommand + " WHERE PCID = " + pcid +
" and DisplayName like " + displayName +
" and DisplayVersion like " + displayVersion;
var command = new MySqlCommand(sqlCommand, mysqlcon, transaction); var command = new MySqlCommand(sqlCommand, mysqlcon, transaction);
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
if (valuenames[i] != "PCID") { if (valuenames[i] != "PCID") {
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i])); string itemValue = "";
value.TryGetValue(valuenames[i], out itemValue);
command.Parameters.AddWithValue("$" + valuenames[i], itemValue);
} }
} }
Console.WriteLine(sqlCommand); Console.WriteLine(sqlCommand);

View File

@ -1,19 +1,23 @@
namespace ProgrammList.sql { using System.Collections.Generic;
namespace ProgrammList.sql {
interface SqlBase { interface SqlBase {
internal bool GetSingleLine(string pcid, string program, string version); bool GetSingleLine(string pcid, string program, string version);
internal void CheckTableExists(); void CheckTableExists();
internal void InsertData(Dictionary<string, string> valuesqlCommand);
internal void InsertOrUpdateData(Dictionary<string, string> value);
internal void DeleteOldData(string hostname); void InsertData(Dictionary<string, string> valuesqlCommand);
internal void UpdateData(Dictionary<string, string> value);
void InsertOrUpdateData(Dictionary<string, string> value);
void DeleteOldData(string hostname);
void UpdateData(Dictionary<string, string> value);
} }

View File

@ -1,6 +1,8 @@
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
namespace ProgrammList.sql { namespace ProgrammList.sql {
@ -59,11 +61,15 @@ namespace ProgrammList.sql {
} }
} }
bool SqlBase.GetSingleLine(string pcid, string program, string version) { void SqlBase.CheckTableExists() {
throw new NotImplementedException(); throw new NotImplementedException();
} }
void SqlBase.CheckTableExists() { void SqlBase.DeleteOldData(string hostname) {
throw new NotImplementedException();
}
bool SqlBase.GetSingleLine(string pcid, string program, string version) {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -75,10 +81,6 @@ namespace ProgrammList.sql {
throw new NotImplementedException(); throw new NotImplementedException();
} }
void SqlBase.DeleteOldData(string hostname) {
throw new NotImplementedException();
}
void SqlBase.UpdateData(Dictionary<string, string> value) { void SqlBase.UpdateData(Dictionary<string, string> value) {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,4 +1,6 @@
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Configuration; using System.Configuration;
namespace ProgrammList.sql { namespace ProgrammList.sql {
@ -42,7 +44,8 @@ namespace ProgrammList.sql {
string result = ""; string result = "";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuesqlCommand.GetValueOrDefault(valuenames[i]); string value = "";
result += valuesqlCommand.TryGetValue(valuenames[i], out value);
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += ","; result += ",";
@ -61,7 +64,13 @@ namespace ProgrammList.sql {
} }
public void InsertOrUpdateData(Dictionary<string, string> value) { public void InsertOrUpdateData(Dictionary<string, string> value) {
if (GetSingleLine(value.GetValueOrDefault("PCID"), value.GetValueOrDefault("DisplayName"), value.GetValueOrDefault("DisplayVersion"))) { string pcid = "";
string displayName = "";
string displayVersion = "";
value.TryGetValue("PCID", out pcid);
value.TryGetValue("DisplayName", out displayName);
value.TryGetValue("DisplayVersion", out displayVersion);
if (GetSingleLine(pcid, displayName, displayVersion)) {
Console.WriteLine("Update"); Console.WriteLine("Update");
UpdateData(value); UpdateData(value);
@ -86,7 +95,9 @@ namespace ProgrammList.sql {
string result = "set "; string result = "set ";
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
result += valuenames[i] + " = " + value.GetValueOrDefault(valuenames[i]); string res = "";
value.TryGetValue(valuenames[i], out res);
result += valuenames[i] + " = " + res;
if (i < valuenames.Length - 1) { if (i < valuenames.Length - 1) {
result += " ,"; result += " ,";
@ -94,15 +105,23 @@ namespace ProgrammList.sql {
} }
sqlCommand = sqlCommand + result; sqlCommand = sqlCommand + result;
sqlCommand = sqlCommand + " WHERE PCID = " + value.GetValueOrDefault("PCID") + string pcid = "";
" and DisplayName like " + value.GetValueOrDefault("DisplayName") + value.TryGetValue("PCID", out pcid);
" and DisplayVersion like " + value.GetValueOrDefault("DisplayVersion"); string displayName = "";
value.TryGetValue("DisplayName", out displayName);
string displayVersion = "";
value.TryGetValue("DisplayVersion", out displayVersion);
sqlCommand = sqlCommand + " WHERE PCID = " + pcid +
" and DisplayName like " + displayName +
" and DisplayVersion like " + displayVersion;
var command = new SqliteCommand(sqlCommand, sqlitecon, transaction); var command = new SqliteCommand(sqlCommand, sqlitecon, transaction);
for (int i = 0; i < valuenames.Length; i++) { for (int i = 0; i < valuenames.Length; i++) {
if (valuenames[i] != "PCID") { if (valuenames[i] != "PCID") {
command.Parameters.AddWithValue("$" + valuenames[i], value.GetValueOrDefault(valuenames[i])); string itemValue = "";
value.TryGetValue(valuenames[i], out itemValue);
command.Parameters.AddWithValue("$" + valuenames[i], itemValue);
} }
} }