Solved

How can I specify login information within VB.Net code to allow my app to access directories/folders on another server?

Posted on 2008-10-30
3
761 Views
Last Modified: 2012-05-05
I have the following code:

Public Class Form1
    Dim FullPath As String = "\\MyServer\C$"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListSubDirectories(FullPath)

    End Sub

    Public Sub ListSubDirectories(ByVal FullPath As String)
        Dim currDirectory As New IO.DirectoryInfo(FullPath)

        MsgBox(currDirectory.FullName)

        For Each subDirectory As IO.DirectoryInfo In currDirectory.GetDirectories
            ListSubDirectories(subDirectory.FullName)
        Next
    End Sub
End Class
If I specify the FullPath to be the PC I am working on, there are no issues because I am already logged in obviously. If I specify FullPath as another server, it will list the initially directory I specified, but will go no further. I receive an error message:
IOException was unhandled
Logon failure: unknown user name or bad password.
I would just like to know how to specify username and password within app. My ultimate goal is to create an app which will copy/backup specified folders/directories from specified server to another network resource, ie; another PC, Networked Hard Drive, etc.

Thanks in advance.
0
Comment
Question by:WoolfStephen
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
VegasCid earned 350 total points
ID: 22841102
Hi,

After a while studing the same situation for my job I had to use shell function in vb.net.

Both FileInfo or DirectoryInfo don`t use networkCredential object to autenticate with other computers.

You can access to other computer if your Windows have access to the other computer. You get this rights when you create a network Drive or access by using Windows Explorer and you write your credentials.

Using cmd you can create NetworkDrives with net use command. You can read about it in http://www.computerhope.com/nethlp.htm

I haven't found a direct programming solution but I've get do it with:
Dim path as string = "\\10.10.10.10\c$\test"

Dim user as string = "Domain\user"

Dim pass as string = "password"
 

Shell("NET USE " & path & " " & pass & " /USER:" & user, 0, True)

Shell("NET USE " & path & " /SAVECRED", 0, True)
 

        Dim di As New DirectoryInfo(path )

        ' Get a reference to each directory in that directory.

        Dim diArr As DirectoryInfo() = di.GetDirectories()

        ' Display the names of the directories.

        Dim dri As DirectoryInfo

        For Each dri In diArr

            Console.WriteLine(dri.Name)

        Next dri

Open in new window

0
 
LVL 1

Author Comment

by:WoolfStephen
ID: 22844921
Thanks VegasCID,
I remember writing a script for mapping a drive once that would map the drive everytime the user logged in, so this makes sense. I will give this a shot.

Thx
0
 
LVL 1

Author Closing Comment

by:WoolfStephen
ID: 31511621
Thx for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

13 Experts available now in Live!

Get 1:1 Help Now