?
Solved

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

Posted on 2005-03-14
30
Medium Priority
?
603 Views
Last Modified: 2007-12-19
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
Comment
Question by:cjinsocal581
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 15
30 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 13539996
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13540064
Would this work?:

strPATH = "//fileServer2K/NetworkShareH"
            For Each strFile In Directory.GetFiles(strPATH, 0)
                ListView1.Items.Add(strFile, 0)
            Next
0
 
LVL 7

Expert Comment

by:natloz
ID: 13540113
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:natloz
ID: 13540122
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13540142
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
 
LVL 7

Expert Comment

by:natloz
ID: 13540175
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13540198
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
 
LVL 7

Expert Comment

by:natloz
ID: 13540225
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13540245
I tried that and it gave an error:

"The given path's format is not supported."
0
 
LVL 7

Expert Comment

by:natloz
ID: 13547727
Can I see how you wrote it?

The slashes must be \\ not //
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13547950
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13547959
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
 
LVL 7

Expert Comment

by:natloz
ID: 13547992
Does the computer have Admin rights to the path you are providing?
Is this run over Citrix with the $ being there?
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548032
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
 
LVL 7

Expert Comment

by:natloz
ID: 13548065
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548103
\\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
 
LVL 7

Expert Comment

by:natloz
ID: 13548153
What if you remove the $
What happens?
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548194
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
 
LVL 7

Expert Comment

by:natloz
ID: 13548216
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548250
Just did on a different share that was not hidden and it shows the files in the ListView control.
0
 
LVL 7

Expert Comment

by:natloz
ID: 13548310
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
 
LVL 7

Expert Comment

by:natloz
ID: 13548416
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548480
Let me try it and see if it works.
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548831
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
 
LVL 7

Expert Comment

by:natloz
ID: 13548859
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13548888
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
 
LVL 7

Accepted Solution

by:
natloz earned 2000 total points
ID: 13549096
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13560913
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
 
LVL 7

Expert Comment

by:natloz
ID: 13565586
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
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13565680
Will do. Thanks for all the help.
0

Featured Post

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!

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 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