-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
170 lines (137 loc) · 6.38 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
namespace lefttimeapp
{
public partial class Form1 : Form
{
private static int WM_QUERYENDSESSION = 0x11;
public Form1()
{
InitializeComponent();
this.FormClosing += Form1_FormClosing;
this.Activated += new System.EventHandler(this.Form1_Activated);
/* this.Hide();
this.Visible = false;*/
}
private void Form1_Activated(object sender, EventArgs e)
{
/*this.Hide()*/;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
this.Visible = false;
this.Opacity = 0;
btnReadFile_Click();
/* this.ShowInTaskbar = false;*/
}
private void btnReadFile_Click()
{
// Get the path of the application's startup folder
string appFolderPath = Application.StartupPath;
// Specify the filename you want to read (change it accordingly)
string filename = "userDetails.dat";
// Combine the folder path and filename to get the full file path
string filePath = Path.Combine(appFolderPath, filename);
Console.WriteLine(filePath);
try
{
// Check if the file exists
if (File.Exists(filePath))
{
// Read the contents of the file
string fileContent = File.ReadAllText(filePath);
JObject jsonObject = JObject.Parse(fileContent);
// Display the content in a MessageBox (you can use any other way to display it)
int userId = (int)jsonObject["userId"];
int tenantId = (int)jsonObject["tenantId"];
// Display the values (you can use any other way to display them)
MessageBox.Show($"User ID: {userId}\nTenant ID: {tenantId}", "JSON Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("File not found.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
string message = "System is shutting down or user is logging off.1 WndProc";
// Get the path to the Documents folder
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Specify the file path in the Documents folder
string filePath = Path.Combine(documentsPath, "shutdown_log1.txt");
// Write the message to a text file
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine($"{DateTime.Now} - {message}");
}
// MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
}
base.WndProc(ref m);
}
/* private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Hello");
this.Hide();
}*/
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Console.WriteLine("Hello Form1_FormClosed");
string message = "System is shutting down or user is logging off.1 WndProc";
// Get the path to the Documents folder
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Specify the file path in the Documents folder
string filePath = Path.Combine(documentsPath, "shutdown_log1.txt");
// Write the message to a text file
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine($"{DateTime.Now} - {message}");
}
// MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
//In case windows is trying to shut down, don't hold the process up
if (e.CloseReason == CloseReason.UserClosing)
{
Console.WriteLine("Hello Form1_FormClosing UserClosing");
string message = "System is shutting down or user is logging off.1 WndProc";
// Get the path to the Documents folder
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Specify the file path in the Documents folder
string filePath = Path.Combine(documentsPath, "close_log1.txt");
// Write the message to a text file
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine($"{DateTime.Now} - {message}");
}
/* MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot UserClosing");*/
}
// Prompt user to save his data
if (e.CloseReason == CloseReason.WindowsShutDown)
{
Console.WriteLine("Hello Form1_FormClosing WindowsShutDown");
string message = "System is shutting down or user is logging off.1 WndProc";
// Get the path to the Documents folder
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Specify the file path in the Documents folder
string filePath = Path.Combine(documentsPath, "shutdown_log1.txt");
// Write the message to a text file
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine($"{DateTime.Now} - {message}");
}
/* MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot WindowsShutDown");*/
}
}
}
}