Link to home
Start Free TrialLog in
Avatar of LeibNiZ
LeibNiZ

asked on

Send DOS command with VB.NET

Hi,

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

        Try
            If Not File.Exists("C:\WINNT\SYSTEM32\net.exe") Then
                lblError.Text = "Could not locate drive mapping tool. Contact Patrick Poulin."
                Return False
            Else
                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
                mapDriveProcess.Start()
                mapDriveProcess.WaitForExit()
                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 !
LeibNiZ
Avatar of LeibNiZ
LeibNiZ

ASKER

I'm using this in a asp.net 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
LeibNiZ
wouldn't something like

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

work for you?

Avatar of LeibNiZ

ASKER

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 !
Try this
WaitForSingleObject

Ref: http://www.thescarms.com/VBasic/wait.asp


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)
           Else
                lngRet = -1
           End If
    End If
Avatar of LeibNiZ

ASKER

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)
                        CloseHandle(lHnd)
                    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 ?
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?

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.

Matteo
Avatar of LeibNiZ

ASKER

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.

Thx,
LeibNiZ
Avatar of Bob Learned
There is an optional timeout value for WaitForExit.

Bob
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#

Avatar of LeibNiZ

ASKER

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 ??
Does it work from VB.Net in Visual Studio (and not ASP).
Avatar of LeibNiZ

ASKER

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 asp.net 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 !?!?
ASKER CERTIFIED SOLUTION
Avatar of matteo_vaccari
matteo_vaccari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial