Skip to content

Commit a38fd59

Browse files
markpmarkp
markp
authored and
markp
committed
Add project files.
1 parent 1427080 commit a38fd59

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

Diff for: GetUserName.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<ApplicationManifest>app.manifest</ApplicationManifest>
9+
</PropertyGroup>
10+
11+
</Project>

Diff for: GetUserName.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetUserName", "GetUserName.csproj", "{14F8A325-F18A-4DD8-89FA-A96A2C54458B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{14F8A325-F18A-4DD8-89FA-A96A2C54458B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{14F8A325-F18A-4DD8-89FA-A96A2C54458B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{14F8A325-F18A-4DD8-89FA-A96A2C54458B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{14F8A325-F18A-4DD8-89FA-A96A2C54458B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {0861EA9B-C3A6-4E7B-A758-F017B64F566C}
24+
EndGlobalSection
25+
EndGlobal

Diff for: Program.cs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using System.Security.AccessControl;
5+
6+
namespace GetUsernameValue
7+
{
8+
class GetUsername()
9+
{
10+
// Import the required Win32 API functions
11+
[DllImport("advapi32.dll", SetLastError = true)]
12+
public static extern bool GetUserName(System.Text.StringBuilder lpBuffer, ref int lpLength);
13+
14+
static void Main(string[] args)
15+
{
16+
// Get all drives on the system
17+
DriveInfo[] drives = DriveInfo.GetDrives();
18+
19+
foreach (DriveInfo drive in drives)
20+
{
21+
Console.WriteLine($"Watching drive: {drive.Name}");
22+
SetupWatcher(drive.Name);
23+
}
24+
25+
// Exit the GetUserName Application
26+
Console.WriteLine("Press 'q' to quit the application.");
27+
while (Console.ReadKey().Key != ConsoleKey.Q) ;
28+
}
29+
30+
static void SetupWatcher(string directoryPath)
31+
{
32+
FileSystemWatcher watcher = new FileSystemWatcher(directoryPath);
33+
34+
watcher.Filter = "*.log";
35+
watcher.IncludeSubdirectories = true;
36+
37+
watcher.Changed += OnChanged;
38+
watcher.Created += OnChanged;
39+
watcher.Deleted += OnChanged;
40+
watcher.Renamed += OnRenamed;
41+
42+
try
43+
{
44+
watcher.EnableRaisingEvents = true;
45+
}
46+
catch (Exception ex)
47+
{
48+
Console.WriteLine($"Error: {ex.Message}");
49+
}
50+
}
51+
private static void OnChanged(object sender, FileSystemEventArgs e)
52+
{
53+
Console.WriteLine($"File: {e.FullPath} {e.ChangeType} at {DateTime.Now}");
54+
55+
try
56+
{
57+
// Get the Group who changed the file
58+
string username = GetFileUsername(e.FullPath);
59+
Console.WriteLine($"User: {username}");
60+
}
61+
catch (Exception ex)
62+
{
63+
Console.WriteLine($"Error: {ex.Message}");
64+
}
65+
try
66+
{
67+
// Get the user who changed the file
68+
string user = Environment.UserName;
69+
Console.WriteLine($"User: {user} {e.ChangeType}");
70+
}
71+
catch (Exception ex)
72+
{
73+
Console.WriteLine($"Error: {ex.Message}");
74+
}
75+
}
76+
77+
private static void OnRenamed(object sender, RenamedEventArgs e)
78+
{
79+
Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath} at {DateTime.Now}");
80+
81+
try
82+
{
83+
// Get the Group who changed the file
84+
string username = GetFileUsername(e.FullPath);
85+
Console.WriteLine($"User: {username}");
86+
}
87+
catch (Exception ex )
88+
{
89+
Console.WriteLine($"Error: {ex.Message}");
90+
}
91+
try
92+
{
93+
// Get the user who changed the file
94+
string user = Environment.UserName;
95+
Console.WriteLine($"User: {user} renamed a file.");
96+
}
97+
catch (Exception ex)
98+
{
99+
Console.WriteLine($"Error: {ex.Message}");
100+
}
101+
}
102+
103+
// Function to get the username of the user who changed the file
104+
private static string GetFileUsername(string filePath)
105+
{
106+
// Get the security information of the file
107+
FileInfo fileInfo = new FileInfo(filePath);
108+
FileSecurity fileSecurity = fileInfo.GetAccessControl();
109+
110+
// Get the owner of the file
111+
string owner = fileSecurity.GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
112+
113+
// Extract the username
114+
string[] split = owner.Split('\\');
115+
string username = split[1];
116+
return username;
117+
}
118+
}
119+
}

Diff for: app.manifest

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC Manifest Options
8+
If you want to change the Windows User Account Control level replace the
9+
requestedExecutionLevel node with one of the following.
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
Specifying requestedExecutionLevel element will disable file and registry virtualization.
16+
Remove this element if your application requires this virtualization for backwards
17+
compatibility.
18+
-->
19+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- A list of the Windows versions that this application has been tested on
27+
and is designed to work with. Uncomment the appropriate elements
28+
and Windows will automatically select the most compatible environment. -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
35+
36+
<!-- Windows 8 -->
37+
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
38+
39+
<!-- Windows 8.1 -->
40+
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
41+
42+
<!-- Windows 10 -->
43+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
49+
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
50+
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
51+
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
52+
53+
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
54+
<!--
55+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
56+
<windowsSettings>
57+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
58+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
59+
</windowsSettings>
60+
</application>
61+
-->
62+
63+
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
64+
<!--
65+
<dependency>
66+
<dependentAssembly>
67+
<assemblyIdentity
68+
type="win32"
69+
name="Microsoft.Windows.Common-Controls"
70+
version="6.0.0.0"
71+
processorArchitecture="*"
72+
publicKeyToken="6595b64144ccf1df"
73+
language="*"
74+
/>
75+
</dependentAssembly>
76+
</dependency>
77+
-->
78+
79+
</assembly>

0 commit comments

Comments
 (0)