How to access file(s) using a UNC path with credentials in VB.NET

I'm writing a VB.NET app which needs access to read and write text files in a folder on a remote server.  While I could just map a drive letter on the local machine, this would give anyone who started up Explorer or My Computer access to this folder.  I thought about running an FTP server on the remote server and having the VB program PUT and GET files but it's not as fast as a drive letter mapping.

So I am wondering if it is possible to reference the file(s) simply form a UNC path where I could name the file I want and pass some credentials to gain access to the file.  

Can this be done or does anyone have anothert suggestion?  
Who is Participating?
Check this link and make sure your code is complete and correct
mskvareninaAuthor Commented:
I've looked at the links. I'm still a little confused as to how I Read/Write files on the remote server using the impersonated user and the UNC path.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

The links should provide you plenty information. Some more links. These are related to ASP.NET but the concept is same across web and windows apps.
mskvareninaAuthor Commented:
I have the impersonation working(at least I do not get any errors). When I try to read a file from the remote server using UNC format, I get an error that access is denied. The UNC I use is in the format of
Firstly, i think you should use this path

The error is self explanatory!
mskvareninaAuthor Commented:
First, I am using \\999.999.999.999\ format. Sorry about the slashs.

When I do the Impersonate User, the function returns as successfull. No Errors.
The LogonUser function completes with no error.
The user/password exists on both the client and server.

    Public Sub ImpersonateUser(ByVal Impersonate As Boolean)

        Dim newidentity As WindowsIdentity
        If Impersonate = True Then
            newidentity = GetWindowsIdentity(UserName, Domain, Password)
            NewContext = newidentity.Impersonate
        End If
    End Sub

    Private Function GetWindowsIdentity(ByVal UserName As String, _
            ByVal Domain As String, ByVal Password As String) As WindowsIdentity

        Dim SecurityToken As Integer
        Dim Success As Boolean

        Success = LogonUser(UserName, Domain, Password, Logon.Network, Provider.UserDefault, SecurityToken)

        If Not Success Then
            Throw New System.Exception("Logon Failed. Error: " & _
        End If

        GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
        ' Gets to here with no error!!!
    End Function

Dim fileName as String  = "\\999.999.999.999\MyFolder\MyFile.txt"
Dim DataStream As FileStream
      DataStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
      Catch UnauthorizedAccessEx As UnauthorizedAccessException
      Return ERROR_DATA_FILE_READ_OPEN   'Enum with error #

End Try
mskvareninaAuthor Commented:
I understand the error. What I am trying to get is why, after I do the impersonation(If I am doing it correctly), that I still get the access denied error. The user I am impersonating has full access to the remote server, folder and files.
mskvareninaAuthor Commented:
Thanks for your help. Your last link started me on the correct path. One of the threads from that link pointed to another thread that had the answer. I had all of my code correct, but needed to change permission(s) for the current user on the local machine. The current user must have Act as part of the operating system permission. After changing this, the code worked fine.
I thought you would accept the answer then.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.