Buon pomeriggio, ho un problema indescrivibile. Io sto sviluppando un software timesheet, ed in esso, c'è anche un "licenze manager" per gestire le licenze attive.
Il software quando faccio il debug ed lo eseguo dalla cartella del progetto tutto va bene, infatti esce "seriale attiva" e quindi tutto a posto.
Mentre se sposto l'intera cartella "Debug" in un'altra posizione esce un errore (che quivale a: 'seriale non trovata nel database') ma esiste nel database (come mostrato nelle foto):
.
Il codie sorcente è un pò lungo:
using MySqlConnector;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Media;
using Rebex.Net;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
namespace Sliding_Menu_1._0
{
public partial class login : Form
{
public bool IsUserAdministrator()
{
//bool value to hold our return value
bool isAdmin;
WindowsIdentity user = null;
try
{
//get the currently logged in user
user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException ex)
{
isAdmin = false;
}
catch (Exception ex)
{
isAdmin = false;
}
finally
{
if (user != null)
user.Dispose();
}
return isAdmin;
}
static string sr = "";
public static int us;
public static int us1 = 0;
public login()
{
InitializeComponent();
}
private void guna2ControlBox1_Click(object sender, EventArgs e)
{
Application.Exit();
Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
if (guna2HtmlLabel3.Text == "Inattivo")
{
dettagli dt = new dettagli("Error1");
dt.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Err. Seriale")
{
dettagli dt = new dettagli("Error2");
dt.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Err. Seriale 2")
{
dettagli dt = new dettagli("Error3");
dt.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Bloccato")
{
dettagli dt = new dettagli("Error4");
dt.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Scaduto")
{
dettagli dt = new dettagli("Error5");
dt.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Attivo")
{
dettagli dt = new dettagli("Good1");
dt.ShowDialog();
}
}
private void login_Load(object sender, EventArgs e)
{
if(!IsUserAdministrator())
{
var exeName = Process.GetCurrentProcess().MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
startInfo.Verb = "runas";
startInfo.Arguments = "restart";
Process.Start(startInfo);
Application.Exit();
}
try
{
Ntp.SynchronizeSystemClock("it.pool.ntp.org");
bool bloccato = false;
bloccato:
if (bloccato)
{
}
else
{
string em = string.Empty;
string pw = string.Empty;
if (File.Exists("C:\\Users\\" + Environment.UserName + @"\STAGI\Timesheet\Key.gtt"))
{
string line;
// //var text = Console.ReadLine();
int counter = 0;
System.IO.StreamReader file =
new System.IO.StreamReader("C:\\Users\\" + Environment.UserName + @"\STAGI\Timesheet\Key.gtt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("[email]:"))
{
//Console.WriteLine(line.ToString().Substring(Config.Users_lenght));
em = line.ToString().Substring(8);
}
if (line.Contains("[pass]:"))
{
//Console.WriteLine(line.ToString().Substring(Config.Users_lenght));
pw = line.ToString().Substring(7);
break;
}
counter++;
}
file.Close();
}
Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlTop);
Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlRight);
Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlBottom);
Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlLeft);
if (!System.IO.File.Exists(Percorsi.TempP + "Key.gtt"))
{
guna2HtmlLabel3.Text = "Inattivo";
guna2HtmlLabel3.ForeColor = Color.Red;
button2.Text = "Login";
}
else
{
Func<string> SystemId = () =>
{
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorID"].ToString();
}
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string motherBoard = "";
foreach (ManagementObject mo in moc)
{
motherBoard = (string)mo["SerialNumber"];
}
string uniqueSystemId = id + motherBoard;
return uniqueSystemId;
};
bool SerialeNull = false;
string type = "";
Console.WriteLine(SystemId());
string sr = "";
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxxabase=bue8shxaexlujpptoeko;password=65jjyLMzAZFcK1Vex0Pr");
cc.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE email='" + em + "' and pass='" + pw + "' and hwid='" + SystemId() + "'", cc);
MySqlDataReader rd = cmd.ExecuteReader();
if (!rd.HasRows)
{
guna2HtmlLabel3.Text = "Err. Seriale";
button2.Text = "Login";
}
else
{
while (rd.Read())
{
if (rd.IsDBNull(2))
{
SerialeNull = true;
}
if (rd.GetString(4).ToString() == "si")
{
guna2HtmlLabel3.Text = "Bloccato";
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "L'account è stato disattivato, si prega di contattare: software@stagiservizitecnici.com";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
sr = rd.GetString(2).ToString();
message2.ShowDialog();
bloccato = true;
cc.Close();
goto bloccato;
}
}
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc1.Open();
MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM srl WHERE serials='" + Properties.Settings.Default.sr.ToString() + "'", cc1);
MySqlDataReader rd1 = cmd1.ExecuteReader();
if (SerialeNull == true)
{
guna2HtmlLabel3.Text = "Inattivo";
button2.Text = "Inserisci Seriale";
cc.Close();
cc1.Close();
}
else
{
if (!rd1.HasRows)
{
guna2HtmlLabel3.Text = "Err. Seriale 2";
}
else
{
while (rd1.Read())
{
type = rd1.GetString(5);
DateTimeFormatInfo dtfi = (DateTimeFormatInfo)DateTimeFormatInfo.CurrentInfo.Clone();
string srsr = rd1.GetString(0).ToString();
string nwstring = DateTime.Now.ToString();
string exstring = rd1.GetString(1).ToString();
string now = DateTime.Now.ToString("dd/MM/yyyy");
DateTime nwt = DateTime.Parse(now);
DateTime expiry = DateTime.ParseExact(exstring, "dd/MM/yyyy", dtfi);
if (nwt.Date > expiry.Date)
{
guna2HtmlLabel3.Text = "Scaduto";
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
cc.Close();
cc1.Close();
break;
}
else
{
if (rd1.GetString(4) != "")
{
if (rd1.GetString(4) != SystemId())
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.Save();
Properties.Settings.Default.message = "Seriale non valida!";
Properties.Settings.Default.Save();
message_showing message22 = new message_showing();
message22.ShowDialog();
cc.Close();
cc1.Close();
break;
}
else
{
guna2Button1.Enabled = true;
guna2Button2.Enabled = true;
guna2HtmlLabel3.Text = "Attivo";
Properties.Settings.Default.sr = srsr;
Properties.Settings.Default.Save();
UtentiList.tipo = type;
button2.Text = "Logout";
Properties.Settings.Default.dts = exstring.ToString();
guna2HtmlLabel3.ForeColor = Color.Green;
Properties.Settings.Default.Save();
cc.Close();
cc1.Close();
break;
}
}
else if (rd1.GetString(4) == null || rd1.GetString(4) == "")
{
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc111.Open();
MySqlCommand cmd111 = new MySqlCommand("UPDATE srl SET hwid='" + SystemId().ToString() + "' WHERE serials='" + sr + "'", cc111);
cmd111.ExecuteNonQuery();
guna2Button1.Enabled = true;
guna2Button2.Enabled = true;
guna2HtmlLabel3.Text = "Attivo";
Properties.Settings.Default.sr = srsr;
Properties.Settings.Default.Save();
UtentiList.tipo = type;
button2.Text = "Logout";
Properties.Settings.Default.dts = exstring.ToString();
guna2HtmlLabel3.ForeColor = Color.Green;
Properties.Settings.Default.Save();
cc111.Close();
cc.Close();
cc1.Close();
break;
}
}
//guna2HtmlLabel4.Text = usage.ToString() + " su " + usagemax.ToString();
}
}
cc.Close();
cc1.Close();
rd.Close();
rd1.Close();
}
cc.Close();
cc1.Close();
rd.Close();
rd1.Close();
}
cc.Close();
rd.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
static void test123(int usesmax)
{
try
{
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc11.Open();
MySqlCommand cmd1111 = new MySqlCommand("UPDATE srl SET uses='" + usesmax.ToString() + "' WHERE serials='" + Properties.Settings.Default.sr.ToString() + "'", cc11);
cmd1111.ExecuteNonQuery();
cc11.Close();
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "Login")
{
key k = new key();
k.ShowDialog();
}
if (guna2HtmlLabel3.Text == "Inattivo" && button2.Text == "Inserisci Seriale")
{
key2 k = new key2();
k.ShowDialog();
}
if (button2.Text == "Riprova")
{
key2 k = new key2();
k.ShowDialog();
}
if (button2.Text == "Login" && guna2HtmlLabel3.Text == "Err. Seriale")
{
key k = new key();
k.ShowDialog();
}
if (button2.Text == "Logout")
{
test123(us1);
File.Delete(Percorsi.TempP + "\\Key.gtt");
Directory.Delete(Percorsi.TempP);
Directory.Delete(Percorsi.TempP2);
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Successo";
Properties.Settings.Default.typeo = "timeout";
Properties.Settings.Default.Save();
Properties.Settings.Default.message = "Logout effettuato!";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
}
}
private void guna2Button1_Click(object sender, EventArgs e)
{
if (Properties.Settings.Default.isConfigured == "False")
{
c cfg1 = new c();
cfg1.ShowDialog();
}
else
{
try
{
Ntp.SynchronizeSystemClock("it.pool.ntp.org");
bool bloccato = false;
bloccato:
if (bloccato)
{
}
else
{
string em = string.Empty;
string pw = string.Empty;
if (File.Exists("C:\\Users\\" + Environment.UserName + @"\STAGI\Timesheet\Key.gtt"))
{
string line;
// //var text = Console.ReadLine();
int counter = 0;
System.IO.StreamReader file =
new System.IO.StreamReader("C:\\Users\\" + Environment.UserName + @"\STAGI\Timesheet\Key.gtt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("[email]:"))
{
//Console.WriteLine(line.ToString().Substring(Config.Users_lenght));
em = line.ToString().Substring(8);
}
if (line.Contains("[pass]:"))
{
//Console.WriteLine(line.ToString().Substring(Config.Users_lenght));
pw = line.ToString().Substring(7);
break;
}
counter++;
}
file.Close();
}
Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlTop);
Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlRight);
Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlBottom);
Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.FromArgb(0, 102, 204) };
this.Controls.Add(pnlLeft);
if (!System.IO.File.Exists(Percorsi.TempP + "Key.gtt"))
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
}
else
{
Func<string> SystemId = () =>
{
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorID"].ToString();
}
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string motherBoard = "";
foreach (ManagementObject mo in moc)
{
motherBoard = (string)mo["SerialNumber"];
}
string uniqueSystemId = id + motherBoard;
return uniqueSystemId;
};
bool SerialeNull = false;
Console.WriteLine(SystemId());
string sr = "";
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE email='" + em + "' and pass='" + pw + "' and hwid='" + SystemId() + "'", cc);
MySqlDataReader rd = cmd.ExecuteReader();
if (!rd.HasRows)
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
}
else
{
while (rd.Read())
{
if (rd.IsDBNull(2))
{
SerialeNull = true;
}
if (rd.GetString(4).ToString() == "si")
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
sr = rd.GetString(2).ToString();
bloccato = true;
cc.Close();
goto bloccato;
}
}
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc1.Open();
MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM srl WHERE serials='" + Properties.Settings.Default.sr.ToString() + "'", cc1);
MySqlDataReader rd1 = cmd1.ExecuteReader();
if (SerialeNull == true)
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
cc.Close();
cc1.Close();
}
else
{
if (!rd1.HasRows)
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
}
else
{
while (rd1.Read())
{
DateTimeFormatInfo dtfi = (DateTimeFormatInfo)DateTimeFormatInfo.CurrentInfo.Clone();
string srsr = rd1.GetString(0).ToString();
string nwstring = DateTime.Now.ToString();
string exstring = rd1.GetString(1).ToString();
string now = DateTime.Now.ToString("dd/MM/yyyy");
DateTime nwt = DateTime.Parse(now);
DateTime expiry = DateTime.ParseExact(exstring, "dd/MM/yyyy", dtfi);
if (nwt.Date > expiry.Date)
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
cc.Close();
cc1.Close();
break;
}
else
{
if (rd1.GetString(4) != "")
{
if (rd1.GetString(4) != SystemId())
{
SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.error);
simpleSound.Play();
Properties.Settings.Default.typeo = "Errore"; Properties.Settings.Default.title = "Errore";
Properties.Settings.Default.Save();
Properties.Settings.Default.typeo = "";
Properties.Settings.Default.message = "Impossibile avviare il Timesheet 2.0";
Properties.Settings.Default.Save();
message_showing message2 = new message_showing();
message2.ShowDialog();
Properties.Settings.Default.dts = "";
Properties.Settings.Default.Save();
cc.Close();
cc1.Close();
break;
}
else
{
Hide();
login_form lg = new login_form();
lg.Show();
cc.Close();
cc1.Close();
break;
}
}
else if (rd1.GetString(4) == null || rd1.GetString(4) == "")
{
MySqlConnection cc111 = new MySqlConnection("server=xxxxxxx-mysql.services.clever-cloud.com;user=xxxx;database=xxx;password=xxx");
cc111.Open();
MySqlCommand cmd111 = new MySqlCommand("UPDATE srl SET hwid='" + SystemId().ToString() + "' WHERE serials='" + sr + "'", cc111);
cmd111.ExecuteNonQuery();
Hide();
login_form lg = new login_form();
lg.Show();
cc111.Close();
cc.Close();
cc1.Close();
break;
}
}
//guna2HtmlLabel4.Text = usage.ToString() + " su " + usagemax.ToString();
}
}
cc.Close();
cc1.Close();
rd.Close();
rd1.Close();
}
cc.Close();
cc1.Close();
rd.Close();
rd1.Close();
}
cc.Close();
rd.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Console.WriteLine(Properties.Settings.Default.sr.ToString());
}
}
private void guna2Button2_Click(object sender, EventArgs e)
{
Properties.Settings.Default.isConfigured = "False";
Properties.Settings.Default.Save();
c cfg1 = new c();
cfg1.ShowDialog();
}
}
}
Vi lascio anche la foto dell'errore:
Helppp