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
774 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

17 Experts available now in Live!

Get 1:1 Help Now