-2

I made this code where it copies a file on my computer to the USB attached to the before mention computer. Here is my code:

            FileInfo file = new FileInfo(all_path);

            file.CopyTo(@"E:\tst\test\testing");

When I try using this on a another computer, it does not work because on that computer the USB is a f:\ drive and on my computer it is a E:\ drive. So how do I make it that the code works on every computer. I hope someone can help me

wingerse
  • 3,670
  • 1
  • 29
  • 61
banana lord
  • 22
  • 1
  • 8

3 Answers3

1

The class you'd want to use to develop your solution is the DriveInfo class. There is no way to guarantee it's the same drive easily but you can use options such as the DriveType property to check it's a removable USB drive or the VolumeLabel if you're trying to make it only use one USB stick.

        DriveInfo[] allDrives = DriveInfo.GetDrives();
        foreach (DriveInfo d in allDrives)
        {
            if (d.DriveType == DriveType.Removable && d.VolumeLabel == "MyVolumeLabel")
            {
                FileInfo file = new FileInfo(all_path);
                file.CopyTo(d.Name + @"\tst\test\testing");
            }                    

        }
Mark E
  • 371
  • 2
  • 12
0

If your application is running from the stick get your path like described here:

get path for my .exe

other option is to check the name of you device. Use the code described here:

Path of the USB devices which are connected to the machine?

Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
0

How to detect a USB drive has been plugged in?

This can help you.

When you find Removable Drives you can query all files to find which you want.

Or if you will you the same drive all the time, you can use DriveInfo.Name property.

Community
  • 1
  • 1
TarikGuren
  • 83
  • 1
  • 6