What you are trying to do is not unique.
The best place to start is here:
How make autorun from usb in c# - Google Search[
^]
ChatGPT[
^] also has an interesting response when you ask it:
How make autorun from usb in c#.
The response given was:
Quote:
In order to create an autorun from a USB drive using C#, you can use the System.Management namespace to detect when a USB drive is connected to the computer and then run an executable file from the USB drive. Here's a sample code that demonstrates how to do this:
The c# code example was:
using System;
using System.Management;
using System.IO;
class Program
{
static void Main(string[] args)
{
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
ManagementEventWatcher watcher = new ManagementEventWatcher();
watcher.Query = query;
watcher.EventArrived += new EventArrivedEventHandler(USBInserted);
watcher.Start();
while (true) { }
}
static void USBInserted(object sender, EventArrivedEventArgs e)
{
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
string driveLetter = driveName.Substring(0, 2);
string autorunFile = driveLetter + "autorun.inf";
if (File.Exists(autorunFile))
{
string executablePath = driveLetter + "myprogram.exe";
if (File.Exists(executablePath))
{
System.Diagnostics.Process.Start(executablePath);
}
}
}
}
And the summary:
Quote:
In this code, the USBInserted method is called whenever a USB drive is inserted into the computer. The method checks if the USB drive contains an autorun.inf file and if it does, it runs the myprogram.exe executable file from the USB drive. Note that you'll need to replace myprogram.exe with the name of your own executable file.
I'm not sure exactly what you want to do however, there are tools out there that you can use to research.