Send DOS command with VB.NET

Posted on 2004-10-06
Last Modified: 2012-06-21

I'm trying to execute a DOS command, the "net use" command, but all my efforts worth nothing as of this day... I tried a process, the "Shell" function, the "ShellExecute" function... nothing woked. Sometimes the command is likely to work but it never returns when i put a "waitForExit" like statement. my lastest but meaningless effort is:

    Public Function createDrive() As Boolean
        Dim mapDriveProcess As Process
        Dim logicalDrives As Long
        Dim drive As String = Nothing
        Dim cpt As Long

            If Not File.Exists("C:\WINNT\SYSTEM32\net.exe") Then
                lblError.Text = "Could not locate drive mapping tool. Contact Patrick Poulin."
                Return False
                mapDriveProcess = New Process
                mapDriveProcess.StartInfo.FileName = "C:\WINNT\system32\cmd.exe"
                mapDriveProcess.StartInfo.Arguments = "/C net use B: \\alyson\public\webprod"
                mapDriveProcess.StartInfo.UseShellExecute = False
                'get the available drives to bound to
                logicalDrives = GetLogicalDrives
                For cpt = 0 To 25
                    If (logicalDrives And 2 ^ cpt) = 0 Then
                        drive = Chr(65 + cpt)
                        setDrive(drive + ":")
                        Exit For
                    End If
                Next cpt
                Return True
            End If
        Catch ex As Exception
            lblError.Text = "An error as occured during the binding process."
            Return False
        End Try
    End Function

this function never returns from the "mapDriveProcess.WaitForExit()" statement. I also tried:

mapDriveProcess.StartInfo.FileName = "C:\WINNT\system32\net.exe"
mapDriveProcess.StartInfo.Arguments = "net use B: \\alyson\public\webprod" ...

any help would be appreciated !
Question by:LeibNiZ
  • 6
  • 4
  • 3
  • +1

Author Comment

ID: 12237144
I'm using this in a web page to access a virtual path. I must download files and folders from a server \\alyson\public\webprod to a local machine and i dont wanna work with webClient.downloadFile because i can't download entire folders. If i could bind let's say, B: to \\alyson\public\webprod i could easely work with B:\X15\folder... When run, the web page freeze and the progress bar at the bottom stop from reaching 100%... that's what i meant by "the function never returns"

Thanks in advance

Expert Comment

ID: 12238578
wouldn't something like

System.IO.File.Copy("\\orione\stuff\foo.txt", "c:\temp\foo.txt")

work for you?


Author Comment

ID: 12238955
nope matteo,

this msg appears: "Could not find file: 'blah blah...'"

I need a way for my program to 'see' \\alyson the server. But my question was why can't I start this DOS command ?!
wierd.. even why i put "notepad.exe" the prog don't show up. It's in the process list but not on my desktop !

Expert Comment

ID: 12239702
Try this


Use it as
    lngPid = Shell(strCMDLine, intWindowStyle)
    If lngPid <> 0 Then
           'Get a handle to the shelled process.
           lngHnd = OpenProcess(SYNCHRONIZE, 0, lngPid)
           'If successful, wait for the application to end and close the handle.
           If lngHnd <> 0 Then
                lngRet = WaitForSingleObject(lngHnd, lngWaitPeriodSeconds * 1000)
                CloseHandle (lngHnd)
                lngRet = -1
           End If
    End If

Author Comment

ID: 12240861
not again avi247,

Dim lPid As Long
                Dim lHnd As Long
                Dim lRet As Long

                lPid = Shell("net use B: \\alyson\public\webprod", vbNormalFocus)
                If lPid <> 0 Then
                    'Get a handle to the shelled process.
                    lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
                    'If successful, wait for the application to end and close the handle.
                    If lHnd <> 0 Then
                        lRet = WaitForSingleObject(lHnd, INFINITE)
                    End If
                    'MsgBox("Just terminated.", vbInformation, "Shelled Application")
                End If

doesn'T work... the WaitForSingleObject function just go straight the code and no drive is mapped...
any suggestions ?

Expert Comment

ID: 12241750
Execute the net use command in Start>Run>Cmd window. Does it work?

net use B: \\alyson\public\webprod

Try net use with the username and password also.

Also, check the permissions\max users connection on that share. Try giving full control to that directory structure.

Let me know if it helped.

If you are getting any error, what is it?


Expert Comment

ID: 12246384
The reason why you don't see notepad when started from an asp page has to do with window stations.  IIS is a service running in the system account; as such, it has no access to your desktop.  This is for security reasons, so that services can't snoop what keys you are typing and so on.

I was suggesting to try File.Copy because if it does not work, then it shows that IIS doesn't have access to the alyson server.  

Using "net use" seems way too complicated.  As an alternative, see if alyson has a iis server of its own; you might be able to get the files via http; assuming you are allowed to make this modification to iis configuration, and there are no security reasons not to do it.

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.


Author Comment

ID: 12246923
I also tried via http, it works great if a got only one file to download with WebClient.downloadFile() but there is no method or class that can download entire folders... (what i'd like to do)

is there a way to download folders via http ?
if there is, it would solve my problem.

LVL 96

Expert Comment

by:Bob Learned
ID: 12247828
There is an optional timeout value for WaitForExit.


Expert Comment

ID: 12248171
There is a way to download an entire folder via http.  First you must make sure it is possible to get a directory list from the server; try to get http://alyson/mydirectory/ in your browser.  If you get an error, you must tweak the iis configuration on Alyson to grant "directory browse" permission on that directory.

Then you can download the folder by first downloading the directory list, and then extract links from it using regular expressions.  Even simpler, you can extract the file names using something like

dir = WebClient.downloadFile("http://alyson/mydirectory/")
iStart = dir.IndexOf("href=""", 0)
while iStart <> -1
  iEnd = dir.IndexOf("""", iStart)
  filename = dir.SubString(iStart, iEnd - iStart)
  ' download filename
  iStart = dir.IndexOf("href=""", iEnd)
end while

-- sorry about the syntax; I usually do C#


Author Comment

ID: 12250919
Nope matteo,

you CANNOT download folder via the downloadFile method. It's for downloading files... that's why they called it, "downloadFILE" maybe you can do it in C#, but im with VB.NET and i can't do it like this.

That's why I wanted to call the net use command to bind a drive to the server and then calling method from the FILE and DIRECTORY classes. But i wonder, net use is a DOS command like any other commands. Why can't i start it with the process classe or Shell function ?? if i type "net use B: \\alyson\public\webprod" from the command prompt, it works great. Now i only need to do this in VB.NET... maybe tit wont work cause its from an ASP page... If so, how am i suppose to download folders over the internet ??

Expert Comment

ID: 12251572
Does it work from VB.Net in Visual Studio (and not ASP).

Author Comment

ID: 12252287
hi avi247,

Yes it worked under VB.NET. The command:

Shell("net use B: \\alyson\public\webprod", AppWinStyle.NormalFocus, True)

worked perfecly. seems like it don't like being lauched from a web page...

the same command run in web page never returns... I dont know if it tries binding the drive B: on the server itself ! i don't know... I dont have access to the server... Well i know it dont work cuz the command never returns... How can i make net use to bind the drive to the client computer !?!?

Accepted Solution

matteo_vaccari earned 500 total points
ID: 12257284
Excuse my insistence, but I'm not telling you to download a whole directory.  What I meant was to download a directory _listing_.  When you call any url, such as, the browser does not know or care if what you're getting is the "index.html" document or a directory listing that is built on the fly by the web server.  All you get is html code in both cases.  In the latter case, you can read the listing to extract the names of files.  

And no, it's not a C# versus VB problem.  In .Net you can do pretty much the same things with both languages.



Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now