• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

Need a way to access a network share in VB.NET

I need a way to access a network share in VB.NET without having to create the share on the client PC.

In other words, the "Server" PC has a folder that is shared. I want my App that is loaded on the "Client" PC to access this share without having to create a Mapped network drive.

Is this possible? and if so, how?
0
cjinsocal581
Asked:
cjinsocal581
  • 15
  • 15
1 Solution
 
natlozCommented:
Use UNC naming...

eg. H:/NetworkShareH

could translate to //fileServer2K/NetworkShareH

....this way...any machine that is allowed to MAP the share...can get to the pc

fileServer2K is the network server name...
0
 
cjinsocal581Author Commented:
Would this work?:

strPATH = "//fileServer2K/NetworkShareH"
            For Each strFile In Directory.GetFiles(strPATH, 0)
                ListView1.Items.Add(strFile, 0)
            Next
0
 
natlozCommented:
I have not worked with the Directory.GetFiles command...

A valid UNC path can be replaced for any Mapped path.

Here is a sample from my network...

\\EDMSERVER\SharedData\Human Resources\Resumes\

which is mapped on some machines as

F:\Resumes (F:\ turns into \\EDMSERVER\SharedData\Human Resources\ portion of the path)

They are interchangeable.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
natlozCommented:
The difference is you do not need to MAP a UNC path...it is based on a network machine name that is visible to the client
0
 
cjinsocal581Author Commented:
Would you know how to do that in VB?

In other words, how can I translate:

strPATH = "//fileServer2K/NetworkShareH"

into a valid location for VB to look at like "H:\"
0
 
natlozCommented:
You can't do that...you can translate a MAPPED Drive into a UNC path...but a UNC path cannot be translated into a Mapped drive...

for instance..(Clients can map H: to whatever they want)

Client 1 - H: is mapped to \\fileServer1
Client 2 - H: is mapped to \\printServer6
Client 3 - H: is mapped to \\edmfileServer

Therefore you cannot translate a UNC into a Mapped drive...

what I am saying is for you to abandon Mapped Drives all together...and use UNC paths...

for example...

if the folder you need to get to from any CLIENT machine is on \\fileServer1...use the UNC instead of a MAP drive...

does this make sense?
0
 
cjinsocal581Author Commented:
I got that part, but where I am stuck at is getting the folders or files within  a path.

So do you know a way to view the folders within an UNC path in VB.NET?
0
 
natlozCommented:
If you can do it for a MAPPED drive it is no Different...just replace the Mapped drive path with the UNC path to the same folder...
0
 
cjinsocal581Author Commented:
I tried that and it gave an error:

"The given path's format is not supported."
0
 
natlozCommented:
Can I see how you wrote it?

The slashes must be \\ not //
0
 
cjinsocal581Author Commented:
Private DirectoryPath As String = "\\10.1.1.106\cjdev$ /u:administrator password"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strFile As String
        For Each strFile In Directory.GetDirectories(DirectoryPath, 0)
            ListView1.Items.Add(strFile, 0)
        Next
    End Sub
0
 
cjinsocal581Author Commented:
Then when I take out the "/u:administratoe password"

I get this error:

"An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: Logon failure: unknown user name or bad password."
0
 
natlozCommented:
Does the computer have Admin rights to the path you are providing?
Is this run over Citrix with the $ being there?
0
 
cjinsocal581Author Commented:
I do have administrator rights to it.

The $ means it is a hidden share.

I was told yesterday that I need a full class to impersonate the username and password.
0
 
natlozCommented:
what happens if you use the full path?
To find the full path use windows explorer and go the the final folder through MY NETWORK PLACES
Whatever path is in your Address bar should work...
0
 
cjinsocal581Author Commented:
\\10.1.1.106\cjdev$ - is the path.

I can open it just fine. For some reason, VB.NET needs to impersonate the credentials to connect to it I just do not know how.
0
 
natlozCommented:
What if you remove the $
What happens?
0
 
cjinsocal581Author Commented:
I can't. It needs to be a hidden share. If I remove it from the SERVER's share, it can be located by anyone on the network.
0
 
natlozCommented:
Can you do a test on a non-hidden share (pick any folder on a server and use UNC).
Just to see that UNC is working...
0
 
cjinsocal581Author Commented:
Just did on a different share that was not hidden and it shows the files in the ListView control.
0
 
natlozCommented:
OK...so UNC is working...that is a good step.

The issue is now just passing credentials along with it....
We need to dig around for this one...
0
 
natlozCommented:
If you want to read the file while using different credentials, try this:
System.Threading.Thread.CurrentPrincipal = CType(New
System.Net.NetworkCredential("user","pw"),
System.Security.Principal.IPrincipal)

I found this...maybe could be used???
0
 
cjinsocal581Author Commented:
Let me try it and see if it works.
0
 
cjinsocal581Author Commented:
Here is what I have come up with so far. I just do not know where to put the myCredentials call or if it will even work.
___________________________________________________________________________________________________
Private strPath As String = "\\10.1.1.106\cjdev$"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myCredentials As New System.Net.NetworkCredential("administrator", "password")
        Dim strFile As String, SelectedItem As ListViewItem
        Dim thisFile As FileInfo
        Dim thisDir As New DirectoryInfo(strPath)

        For Each thisFile In thisDir.GetFiles("*.wav")
            SelectedItem = ListView1.Items.Add(thisFile.Name, 0)
            SelectedItem.SubItems.Add(Now())
        Next

    End Sub
______________________________________________________________
0
 
natlozCommented:
Private strPath As String = "\\10.1.1.106\cjdev$"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myCredentials As New System.Net.NetworkCredential("administrator", "password")

        //I would try it here...
        System.Threading.Thread.CurrentPrincipal = CType(myCredentials, System.Security.Principal.IPrincipal)

        Dim strFile As String, SelectedItem As ListViewItem
        Dim thisFile As FileInfo
        Dim thisDir As New DirectoryInfo(strPath)

        For Each thisFile In thisDir.GetFiles("*.wav")
            SelectedItem = ListView1.Items.Add(thisFile.Name, 0)
            SelectedItem.SubItems.Add(Now())
        Next

    End Sub
0
 
cjinsocal581Author Commented:
Here is the error I got.

An unhandled exception of type 'System.InvalidCastException' occurred in UNCShareAccess.exe

Additional information: Specified cast is not valid.

On the System.Threading.Thread.CurrentPrincipal = CType(myCredentials, System.Security.Principal.IPrincipal)
0
 
cjinsocal581Author Commented:
I was able to get the code in my app. But it does not seem to work:
________________________________________________________________________________________
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Principal
Imports System.Threading

Public Class Form1

    Inherits System.Windows.Forms.Form

Private strPath As String = "\\10.1.1.106\cjdev$"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rolesArray As String() = {"managers", "executives"}
        Try
            'Set the principal to a new generic principal.
            Thread.CurrentPrincipal = _
                New GenericPrincipal(New GenericIdentity( _
                "administrator", "password"), rolesArray)

        Catch secureException As SecurityException
            Debug.WriteLine("{0}: Permission to set Principal " & _
               "is denied.", secureException.GetType().Name)
        End Try

        Dim threadPrincipal As IPrincipal = Thread.CurrentPrincipal
        Console.WriteLine( _
            "Name: {0}" & vbCrLf & "IsAuthenticated:" & _
            " {1}" & vbCrLf & "AuthenticationType: {2}", _
            threadPrincipal.Identity.Name, _
            threadPrincipal.Identity.IsAuthenticated, _
            threadPrincipal.Identity.AuthenticationType)

        Debug.WriteLine(System.Threading.Thread.CurrentPrincipal)
        Dim strFile As String, SelectedItem As ListViewItem
        Dim thisFile As FileInfo
        Dim thisDir As New DirectoryInfo(strPath)

        For Each thisFile In thisDir.GetFiles("*.wav")
            SelectedItem = ListView1.Items.Add(thisFile.Name, 0)
            SelectedItem.SubItems.Add(Now())
        Next

    End Sub
___________________________________________________________________________________________________

Any ideas what I am doing wrong? I am just not to familiar with this type of coding on the System.Threads
0
 
natlozCommented:
This is getting into a realm that I have not worked with either.
You may want to create a new post and link to this one...but call it something like...

"How To GetFiles From a Hidden Share Without Mapped Drive"
0
 
cjinsocal581Author Commented:
Will do. Thanks for all the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 15
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now