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
747 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thx for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

10 Experts available now in Live!

Get 1:1 Help Now