Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Send DOS command with VB.NET

Posted on 2004-10-06
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 !
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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.


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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

670 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