locked
Mount a virtual drive? RRS feed

  • Question

  • Hello, I'm interested in making a program that has the ability to mount a folder (e.g. C:\MyFolder\) to a drive letter (e.g. F:\). Is this possible with VB? If so how do I do it? And if not, can it be achieved with C++ or C#?
    Monday, December 18, 2006 10:49 PM

Answers

  • it cant be achieved in C++ not within .NET unless you shell it out or use some SDK/API. .NET is a high level language than C/C++ since you can access the "core" stuff from it. you would need to obtain a free drive letter from the OS then some how make a drive out of it and do all sorts of things, its been discussed previously but its not something you can do within .NET. Perhaps with some 3rd party SDK yes but other than that...its pretty much not so much possible
    Tuesday, December 19, 2006 2:41 AM
  • Sorry, this is VB not C#

    As far as I know this is quite safe to do:

    Imports System.io

    Public Class Form1

     

        ' The string below gets assigned the virtual drive letter.

        ' I have hard coded 'c:\temp' as the target of the virtual

        ' drive letter. Change that as needed.

        Dim AvailDr As String = ""

     

     

        Private Sub Form1_Load(ByVal sender As Object, _

        ByVal e As System.EventArgs) Handles Me.Load

            Dim avail As Integer

     

            ' Get an array of drive letters

            Dim drs() As String = Directory.GetLogicalDrives

            ' Start with drive 'Z' (CHRW(90)) to 'A'

            For x As Integer = 90 To 65 Step -1

                avail = Array.IndexOf(Of String)(drs, ChrW(x) & ":\")

                If avail = -1 Then

                    AvailDr = ChrW(x) & ":"

                    Exit For

                ElseIf x = 65 Then

                    MsgBox("Could not find an available drive")

                    Exit Sub

                End If

            Next

     

            Dim psi As New ProcessStartInfo

            psi.FileName = "cmd.exe"

            ' The good 'ol DOS command 'subst'

            ' Creates a virtual drive letter for

            ' a directory location.

            psi.Arguments = "/c subst " & AvailDr & " c:\temp"

            psi.CreateNoWindow = True

            psi.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(psi)

            ' Before we create or touch any files

            ' in the virtual drive, we need to give

            ' the system time to set it up

     

            ' If anything in the your load uses the

            ' virtual drive immediately, then a

            ' thread sleep is needed to let the

            ' virtual drive 'kick in'

     

            'System.Threading.Thread.Sleep(500)

        End Sub

     

        Private Sub Form1_FormClosing(ByVal sender As Object, _

        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

            ' Remove virtual drive sub

            Dim psi As New ProcessStartInfo

            psi.FileName = "cmd.exe"

            ' Cancel drive letter

            psi.Arguments = "/c subst " & AvailDr & " /d"

            psi.CreateNoWindow = True

            psi.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(psi)

        End Sub

     

    End Class

     

     

    Tuesday, December 19, 2006 5:17 AM

All replies

  • Ok, I understand I can use subst.exe to mount/unmount drives, but is it entirely safe to do so? I've heard horror stories about data corruption from this sort of thing.

    Monday, December 18, 2006 10:59 PM
  • it cant be achieved in C++ not within .NET unless you shell it out or use some SDK/API. .NET is a high level language than C/C++ since you can access the "core" stuff from it. you would need to obtain a free drive letter from the OS then some how make a drive out of it and do all sorts of things, its been discussed previously but its not something you can do within .NET. Perhaps with some 3rd party SDK yes but other than that...its pretty much not so much possible
    Tuesday, December 19, 2006 2:41 AM
  •  

    Let's get a little real here. If you want a list of available Drive letters, all you have to do is make a list

     

    Microsoft.VisualBasic.FileIO.FileSystem.Drives() As System.Collections.ObjectModel.ReadOnlyCollection(Of System.IO.DriveInfo)

     

    Of the drives that are not already taken.

    Tuesday, December 19, 2006 4:59 AM
  • Sorry, this is VB not C#

    As far as I know this is quite safe to do:

    Imports System.io

    Public Class Form1

     

        ' The string below gets assigned the virtual drive letter.

        ' I have hard coded 'c:\temp' as the target of the virtual

        ' drive letter. Change that as needed.

        Dim AvailDr As String = ""

     

     

        Private Sub Form1_Load(ByVal sender As Object, _

        ByVal e As System.EventArgs) Handles Me.Load

            Dim avail As Integer

     

            ' Get an array of drive letters

            Dim drs() As String = Directory.GetLogicalDrives

            ' Start with drive 'Z' (CHRW(90)) to 'A'

            For x As Integer = 90 To 65 Step -1

                avail = Array.IndexOf(Of String)(drs, ChrW(x) & ":\")

                If avail = -1 Then

                    AvailDr = ChrW(x) & ":"

                    Exit For

                ElseIf x = 65 Then

                    MsgBox("Could not find an available drive")

                    Exit Sub

                End If

            Next

     

            Dim psi As New ProcessStartInfo

            psi.FileName = "cmd.exe"

            ' The good 'ol DOS command 'subst'

            ' Creates a virtual drive letter for

            ' a directory location.

            psi.Arguments = "/c subst " & AvailDr & " c:\temp"

            psi.CreateNoWindow = True

            psi.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(psi)

            ' Before we create or touch any files

            ' in the virtual drive, we need to give

            ' the system time to set it up

     

            ' If anything in the your load uses the

            ' virtual drive immediately, then a

            ' thread sleep is needed to let the

            ' virtual drive 'kick in'

     

            'System.Threading.Thread.Sleep(500)

        End Sub

     

        Private Sub Form1_FormClosing(ByVal sender As Object, _

        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

            ' Remove virtual drive sub

            Dim psi As New ProcessStartInfo

            psi.FileName = "cmd.exe"

            ' Cancel drive letter

            psi.Arguments = "/c subst " & AvailDr & " /d"

            psi.CreateNoWindow = True

            psi.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(psi)

        End Sub

     

    End Class

     

     

    Tuesday, December 19, 2006 5:17 AM
  •  robinjam wrote:

    Ok, I understand I can use subst.exe to mount/unmount drives, but is it entirely safe to do so? I've heard horror stories about data corruption from this sort of thing.

    Subst should not cause data corruption. If you have more specific example of what horror stories you heard maybe it is possible to explain it. I can imagine that a common mistake is that a mapped drive is assumed to always be on the same drive letter.

    I will try one scenario that might explain something that can happen with and it is not directly tied to subst.exe, you see that mistake quite often in webpages. The webdeveloper uses an image on the local drive (might be a subst drive but that doesn't really matter) . The link in the HTML points to F:\website\images\logo.jpg. The document is saved to F:\website\index.html

    When uploaded to the website and the developer browses to it everything looks fine because he has F:\website\images.logo.jpg but when anyone else that doesn't have or have another file there the page will be missing a picture or show another picture, it looks corrupted.

    This is not the fault of subst but the developers use of hard links and references.

    I think subst would be a good way to accomplish what you ask for and you have an excellent example how to do it. Another way could be to map it as a network drive but it requires networking components to be setup and working. Here is a link to a C# article.
    http://www.codeproject.com/csharp/mapnetdrive.asp

    Tuesday, December 19, 2006 8:42 AM
  • Thanks all, thats exactly what I was looking for

    Tuesday, December 19, 2006 1:48 PM