Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How make autorun from usb in c#

What I have tried:

How make autorun from usb in c#
Posted
Updated 7-Apr-23 3:36am
v2
Comments
Richard MacCutchan 7-Apr-23 5:13am    
You need to provide much more information about what you are trying to do. As it stands your question is far from clear.

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:
C#
using System;
using System.Management;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Define the query to detect USB drive insertion
        WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");

        // Initialize the watcher for USB drive insertion
        ManagementEventWatcher watcher = new ManagementEventWatcher();
        watcher.Query = query;
        watcher.EventArrived += new EventArrivedEventHandler(USBInserted);

        // Start watching for USB drive insertion
        watcher.Start();

        // Wait for USB drive insertion events
        while (true) { }
    }

    static void USBInserted(object sender, EventArrivedEventArgs e)
    {
        // Get the drive letter of the inserted USB drive
        string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
        string driveLetter = driveName.Substring(0, 2);

        // Check if the USB drive contains an autorun.inf file
        string autorunFile = driveLetter + "autorun.inf";
        if (File.Exists(autorunFile))
        {
            // Get the path to the executable to be run
            string executablePath = driveLetter + "myprogram.exe";

            // Check if the executable file exists
            if (File.Exists(executablePath))
            {
                // Run the executable file
                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.
 
Share this answer
 
v2
If you are trying to make an app autorun when a USB containing it is inserted, that is no longer possible for security reasons - it was disabled in Windows 7 and is very unlikely to be re-enabled.

It was deliberately disabled to curtail the spread of virus and other malware which used it to infect or steal information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900