Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

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
0
LeibNiZ
Asked:
LeibNiZ
  • 6
  • 4
  • 3
  • +1
1 Solution
 
LeibNiZAuthor Commented:
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
0
 
matteo_vaccariCommented:
wouldn't something like

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

work for you?

0
 
LeibNiZAuthor Commented:
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 !
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
avi247Commented:
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
0
 
LeibNiZAuthor Commented:
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 ?
0
 
avi247Commented:
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?

0
 
matteo_vaccariCommented:
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
0
 
LeibNiZAuthor Commented:
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
0
 
Bob LearnedCommented:
There is an optional timeout value for WaitForExit.

Bob
0
 
matteo_vaccariCommented:
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#

0
 
LeibNiZAuthor Commented:
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 ??
0
 
avi247Commented:
Does it work from VB.Net in Visual Studio (and not ASP).
0
 
LeibNiZAuthor Commented:
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 !?!?
0
 
matteo_vaccariCommented:
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 http://www.google.com/, 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.

Matteo


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now