Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Autorun on USB drives

11 Jul 2009   #11
fakeasdf

Win 7 Pro x64 x 3, Win 7 Pro x86, Ubuntu 9.04
 
 

Quote   Quote: Originally Posted by justme View Post
I searched on google and there is no way to make it work Anyway i have multiboot and now i`m using my Vista Ultimate again . Hope the guys from TrueCrypt will do something until Microsoft release the final version of Win7 .

Cya
Try this out... Extract this and run it, if you want it to always start with windows put it in your startup folder. It will check every 2 seconds for a new removable drive, if it has an autorun, it will ask if you want to run it. Lemme know if it works with your TrueCrypt program... If it doesn't just let me know, I can modify it... I don't know what the truecrypt autorun exactly does, I just did a generalization of autoruns when programming it... I didn't add much error protection, so malformed autoruns probably won't work... If there is a reason I might fix it up... but we'll see....


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;



namespace Autorunner
{
    public class RegKeysAndDrives
    {
        public RegistryKey basekey;
        public String subkey;
        public String driveLetter;
        public RegKeysAndDrives(RegistryKey _basekey, String _subkey, String _driveLetter)
        {
            basekey = _basekey;
            subkey = _subkey;
            driveLetter = _driveLetter;
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        public static List<RegKeysAndDrives> toDelete;

        [STAThread]
        static void Main()
        {
            
            //keep track of drives we've already ran, Must compare strings, DriveInfo only compares using the Object
            List<String> haveRun = new List<String>();
            toDelete = new List<RegKeysAndDrives>();
            while (true)
            {
                //let's check every 2 seconds...  We don't want to slow down the machine!
                System.Threading.Thread.Sleep(2000);

                try
                {
                    //Get current drives
                    DriveInfo[] drives = DriveInfo.GetDrives();
                    List<DriveInfo> curDrives = new List<DriveInfo>(drives);

                    //remove any drives from haverun that don't exist anymore
                    for (int i = 0; i < haveRun.Count; i++)
                    {
                        if (DriveInfoContains(curDrives, haveRun[i]) == false)
                        {
                            for (int j = 0;j<toDelete.Count;j++)
                            {
                                if (toDelete[j].driveLetter == haveRun[i])
                                {
                                    try
                                    {
                                        toDelete[j].basekey.DeleteSubKeyTree(toDelete[j].subkey);
                                    }
                                    catch(Exception ex)
                                    {
                                        //subkey doesn't exist anymore :P
                                    }

                                    toDelete.RemoveAt(j);
                                    j--;
                                }
                            }
                            haveRun.RemoveAt(i);
                            i--;
                        }
                    }

                    //let's remove our haveRuns from drives, no point in checking them again...
                    for (int i = 0; i < curDrives.Count; i++)
                    {
                        if (haveRun.Contains(curDrives[i].RootDirectory.Name) == true)
                        {
                            curDrives.RemoveAt(i);
                            i--;
                        }
                    }


                    //Check any new Removable type drives, and run them...
                    foreach (DriveInfo drive in curDrives)
                    {
                        if (drive.DriveType == DriveType.Removable)
                        {
                            if (drive.IsReady)
                            {
                                try
                                {
                                    AutoRunDrive(drive);
                                }
                                catch(Exception ex)
                                {
                                    //I don't care if it crashes trying to launch it, let's just let it go...
                                }
                                haveRun.Add(drive.RootDirectory.Name);
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                    //Let's not crash if something gets unplugged while we're accessing it...
                }
            }

        }

        private static bool DriveInfoContains(List<DriveInfo> curDrives, string haveRunDriveName)
        {
            foreach (DriveInfo di in curDrives)
            {
                if (di.RootDirectory.Name == haveRunDriveName)
                    return true;
            }
            return false;
        }

        private static void AutoRunDrive(DriveInfo drive)
        {
            bool doAutoRun = false;
            String root = drive.RootDirectory.Name;
            String autorun = root + "autorun.inf";
            if (File.Exists(autorun))
            {
                StreamReader sr = new StreamReader(autorun);
                while (!sr.EndOfStream)
                {
                    String line = sr.ReadLine();
                    if (line.ToLower().StartsWith("open"))
                    {
                        int idxEquals = line.IndexOf('=');
                        String toRun = root + line.Substring(idxEquals + 1);
                        
                        //This isn't a good guess of where the args should be, but I don't care
                        int spaceArgs = toRun.IndexOf(' ');
                        String args = toRun.Substring(spaceArgs + 1);
                        args = FixArgs(args,root, toRun.ToLower().Contains("truecrypt"));
                        toRun = toRun.Substring(0, spaceArgs);
                        if (MessageBox.Show("Would you like to Autorun '" + toRun + " " + args + "'?", "Autorunner", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(toRun, args);
                            doAutoRun = true;
                        }
                    }
                    else if(doAutoRun && line.ToLower().StartsWith("shell"))
                    {
                        int equalidx = line.IndexOf("=");
                        String keyName;
                        String subkey;
                        String value;

                        if(equalidx==-1)
                            continue;
                        
                        keyName = line.Substring(5, equalidx - 5);
                        value = line.Substring(equalidx + 1);

                        if (keyName.Contains("command"))
                        {
                            value = root + value;
                        }
                        
                        RegistryKey baseKey = Registry.CurrentUser;
                        subkey = "Software\\Classes\\Drive\\shell" + keyName;
                        RegistryKey key = baseKey.CreateSubKey(subkey);
                        key.SetValue("", value);
                        toDelete.Add(new RegKeysAndDrives(baseKey, subkey, root));
                            
                    }
                }
                sr.Close();
            }
        }

        private static string FixArgs(string args, string root, bool isTrueCrypt)
        {
            if (isTrueCrypt)
            {
                int idxv = args.IndexOf("/v");
                int idxfile = args.IndexOf("\"", idxv);
                return args.Substring(0, idxfile+1) + root + args.Substring(idxfile + 1);
            }
            return args;
            
        }
    }
}





Attached Files
File Type: zip Autorunner.zip (4.9 KB, 400 views)
My System SpecsSystem Spec
.
15 Jul 2009   #12
justme

MultiBoot : Windows 7 Ultimate , Windows Vista Ultimate SP1
 
 

To be more specific, this is my problem :

TrueCrypt have some options that can encrypt a USB stick with a password protect . I bought a 32GB USB stick and i found out that the stick does not have his own password protect program , so i installed TrueCrypt .

Now , TrueCript have an autorun and when you insert the stick you will get this :

Photo1

After i click there i get :

Photo2

On Win7 , i don`t get these options . I have to create a shortcut of TrueCrypt that is on my USB and put some options at target , in order to get that password screen from Photo2 . Or i can run TrueCrypt and mount the partition ( but this require too many clicks )

Also , another thing that i dont get when i use the USB with Win7 is this :

Photo3

When i click there i get the password screen like Photo2.
Everything works on Vista and XP but not on Win7 because of the new autorun security.

Hope you will understand me because i`m from Romania and i don`t know how to write grammatically correct.

Cya

PS: Here is the autorun.inf

[autorun]
label=USB 32GB
icon=icon.ico
action=Mount TrueCrypt volume
open=TrueCrypt\TrueCrypt.exe /q background /lZ /e /m rm /v "TrueCrypt\TrueCrypt.tc"
shell\start=Start TrueCrypt Background Task
shell\start\command=TrueCrypt\TrueCrypt.exe
shell\dismount=Dismount all TrueCrypt volumes
shell\dismount\command=TrueCrypt\TrueCrypt.exe /q /d
My System SpecsSystem Spec
15 Jul 2009   #13
fakeasdf

Win 7 Pro x64 x 3, Win 7 Pro x86, Ubuntu 9.04
 
 

Quote   Quote: Originally Posted by justme View Post
To be more specific, this is my problem :

TrueCrypt have some options that can encrypt a USB stick with a password protect . I bought a 32GB USB stick and i found out that the stick does not have his own password protect program , so i installed TrueCrypt .

Now , TrueCript have an autorun and when you insert the stick you will get this :

Photo1

After i click there i get :

Photo2

On Win7 , i don`t get these options . I have to create a shortcut of TrueCrypt that is on my USB and put some options at target , in order to get that password screen from Photo2 . Or i can run TrueCrypt and mount the partition ( but this require too many clicks )

Also , another thing that i dont get when i use the USB with Win7 is this :

Photo3

When i click there i get the password screen like Photo2.
Everything works on Vista and XP but not on Win7 because of the new autorun security.

Hope you will understand me because i`m from Romania and i don`t know how to write grammatically correct.

Cya

PS: Here is the autorun.inf

[autorun]
label=USB 32GB
icon=icon.ico
action=Mount TrueCrypt volume
open=TrueCrypt\TrueCrypt.exe /q background /lZ /e /m rm /v "TrueCrypt\TrueCrypt.tc"
shell\start=Start TrueCrypt Background Task
shell\start\command=TrueCrypt\TrueCrypt.exe
shell\dismount=Dismount all TrueCrypt volumes
shell\dismount\command=TrueCrypt\TrueCrypt.exe /q /d
I'll look at the shell part of the autorun.inf, did you try out the autorunner program I posted? Did the autorun part start correctly?
My System SpecsSystem Spec
.

18 Jul 2009   #14
justme

MultiBoot : Windows 7 Ultimate , Windows Vista Ultimate SP1
 
 

Yes i did , but there is a problem . TrueCypt mount the partition from temp folder and this is not good. The only thing that works is the VB script from LucienDol , but your program is better . All you have to do is that when i click on yes , truecrypt must mount the partition from the USB , not temp folder . Check the pics. They are made on Win Vista but i get the same thing on Win7.

10x


Attached Images
Autorun on USB drives-1.jpg Autorun on USB drives-2.jpg Autorun on USB drives-3.jpg 
My System SpecsSystem Spec
18 Jul 2009   #15
fakeasdf

Win 7 Pro x64 x 3, Win 7 Pro x86, Ubuntu 9.04
 
 

Quote   Quote: Originally Posted by justme View Post
Yes i did , but there is a problem . TrueCypt mount the partition from temp folder and this is not good. The only thing that works is the VB script from LucienDol , but your program is better . All you have to do is that when i click on yes , truecrypt must mount the partition from the USB , not temp folder . Check the pics. They are made on Win Vista but i get the same thing on Win7.

10x

I see what's going on... The truecrypt program is searching for the volume to mount relative to where my executable is, and not relative to the usb drives root. I've modified the code above to handle it. I'll work on getting the context menus to show up later today, but first I gotta go help my brother move
My System SpecsSystem Spec
19 Jul 2009   #16
fakeasdf

Win 7 Pro x64 x 3, Win 7 Pro x86, Ubuntu 9.04
 
 

Okay, I updated the file above... It now will do the shell extensions and will remove them when the usb drive is removed..
My System SpecsSystem Spec
19 Jul 2009   #17
justme

MultiBoot : Windows 7 Ultimate , Windows Vista Ultimate SP1
 
 

It`s working . Thank you very much
My System SpecsSystem Spec
19 Jul 2009   #18
angelwolf71885

XP SP3
 
 

Quote   Quote: Originally Posted by fakeasdf View Post
Okay, I updated the file above... It now will do the shell extensions and will remove them when the usb drive is removed..

appreciate the work youve done will be useful when i git a new computer..

maybe this can be added to the instruct ables section
My System SpecsSystem Spec
20 Jul 2009   #19
fakeasdf

Win 7 Pro x64 x 3, Win 7 Pro x86, Ubuntu 9.04
 
 

glad it worked out for yall... if there are any issues let me know... the code is above for those who don't want to trust an exe sitting on a website... It's c#...
My System SpecsSystem Spec
03 Aug 2009   #20
hardy

Windows 7 RC
 
 
Simple .bat solution?

This is very old style, but works. This way a mounted Truecrypt volume is just a doubleclick further than with Autoplay.

Create run.bat in the USB stick root and edit this one line into it:

Code:
open=TrueCrypt\TrueCrypt.exe /q background /m rm /v "Data.tc"
This line expects that Truecrypt has been installed (as with the Traveler wizard) in the root in a 'Truecrypt' folder. Rename Data.tc according to the name of your encrypted volume.

Now as Windows 7 opens the 'Open to view files' option, choose that, then double click run.bat to open the Truecrypt password screen.
My System SpecsSystem Spec
Reply

 Autorun on USB drives




Thread Tools




Similar help and support threads
Thread Forum
USB autorun
when my USB is plunged or is opened in the file explorer i want a bat file run how would i do this
General Discussion
DVD/CD won't autorun
I'm pretty sure I have set all the options so that the disks _ought_ to autorun but they won't. I have to go into Powerdesk (File Manager) and select a vob file. I' installed a codec package but that made no difference. I have had problems with this drive, Samsung TSST CDDVDW 288BB, in fact...
Hardware & Devices
No Autorun?
OK, so I'm not sure why this is happening... For some reason it seems that autorun prompts are entirely disabled on my laptop, I'll put in a CD and nothing happens, I plug in a hard drive and nothing happens, etc. I don't get the options window like I did before I did a clean install. I even...
General Discussion
How to Autorun USB on Win 7
Hi, I want to make a USB flash drive auto run a website link in Windows 7. I know the auto run function has been removed in Win 7, but can I at least make a pop up window appear whenever a USB flash drive is inserted in the PC and have the user the option to click and open the link??? ...
Software
USB autorun in W7
Ok, I know this is a sore subject, but are there any work-a-rounds to get handy programs like PortableApps to at least be an option to run when you plug it in? It kind of defeats the purpose of the whole thing. I'm not plugging in rogue USB sticks from strangers in dark alleys, just my own USB...
General Discussion
Autorun.dll
Is missing or corrupted.. anyone have this prob..? I have dl'd and burned the iso 2x and have the same prob. let me say that in order to burn iso..i have just renamed the longnamed file to (the longname).iso i have been dl'ing it with ff and it is adding a ,dlm to the file and i have just been...
Installation & Setup


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 06:38.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App