Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
878 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 1400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

963 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