Jinjinov / Usb.Events

Subscribe to the Inserted and Removed events to be notified when a USB drive is plugged in or unplugged, or when a USB device is connected or disconnected. Usb.Events is a .NET Standard 2.0 library and uses WMI on Windows, libudev on Linux and IOKit on macOS.
MIT License
95 stars 23 forks source link

Inside the windows service this package not working #17

Closed spark6-dev closed 3 years ago

spark6-dev commented 3 years ago

Library version

10.0.0.0

OS & OS version

Windows 10 Pro & 21H1

Describe the bug

Inside the windows service it does not trigger any events with this package.

When we tested this package, it worked in the console application. But this does not work within the Windows services. Is there are any restrictions for this to work inside the Windows services?

To Reproduce

Jinjinov commented 3 years ago

then initialize the functionality inside the OnStart event

These are not exact steps to reproduce the problem.

Please provide exact code to reproduce the problem.

spark6-dev commented 3 years ago

To Reproduce

try
{
    EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service start successfully." });
    using (IUsbEventWatcher usbEventWatcher = new UsbEventWatcher())
    {
        EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { usbEventWatcher.UsbDeviceList.FirstOrDefault().DeviceName.ToString() });
        Console.WriteLine(usbEventWatcher.UsbDeviceList.FirstOrDefault());
        usbEventWatcher.UsbDeviceRemoved += (_, device) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Removed:" + Environment.NewLine + device + Environment.NewLine });

        usbEventWatcher.UsbDeviceAdded += (_, device) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Added:" + Environment.NewLine + device + Environment.NewLine });

        usbEventWatcher.UsbDriveEjected += (_, path) => EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Ejected:" + Environment.NewLine + path + Environment.NewLine });

        usbEventWatcher.UsbDriveMounted += (_, path) =>
        {
            Console.WriteLine("Mounted:" + Environment.NewLine + path + Environment.NewLine);
            EventLog.WriteEvent("Mounted", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Ejected:" + Environment.NewLine + path + Environment.NewLine });
            foreach (string entry in Directory.GetFileSystemEntries(path))
                EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Entries:" + Environment.NewLine + entry + Environment.NewLine });

        };
    }
}
catch (Exception ex)
{
    EventLog.WriteEvent("TestService", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { ex.Message });
}
Jinjinov commented 3 years ago

IUsbEventWatcher usbEventWatcher starts listening for USB events from the moment you call usbEventWatcher = new UsbEventWatcher() and it stops listening for USB events when you call usbEventWatcher.Dispose()

using (IUsbEventWatcher usbEventWatcher = new UsbEventWatcher()) calls usbEventWatcher.Dispose() when the execution reaches } because that is how using works:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

solution:

spark6-dev commented 3 years ago

@Jinjinov: Your efforts are greatly appreciated and thank you for providing some valuable solutions on time. It worked great and your quick response was really appreciated.