Writing to a file located in a local network with user and password using VB

Mansour_Dalir 1,876 Reputation points
2024-08-06T04:22:34.6866667+00:00

How can I write to a file that is located in the local network and has a user and password, using VB? I have already defined the file path, username, and password in the code as follows:

how can we have a read-only folder that can only be edited with VB?

        Dim filePath As String = "\\192.168.80.111\mansour\asd.txt"
        Dim userName As String = "Almas"
        Dim password As String = "00137300"
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,728 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 31,411 Reputation points Microsoft Vendor
    2024-08-06T06:24:27.8266667+00:00

    Hi @Mansour_Dalir ,

    You could consider using the WNetAddConnection2 function from the Windows API to map the network drive with the provided credentials before accessing the file.

        <StructLayout(LayoutKind.Sequential)>
        Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            <MarshalAs(UnmanagedType.LPTStr)>
            Public lpLocalName As String
            <MarshalAs(UnmanagedType.LPTStr)>
            Public lpRemoteName As String
            <MarshalAs(UnmanagedType.LPTStr)>
            Public lpComment As String
            <MarshalAs(UnmanagedType.LPTStr)>
            Public lpProvider As String
        End Structure
    
        <DllImport("mpr.dll", CharSet:=CharSet.Auto)>
        Public Shared Function WNetAddConnection2(ByRef netResource As NETRESOURCE, ByVal password As String, ByVal username As String, ByVal flags As Integer) As Integer
        End Function
    
        <DllImport("mpr.dll", CharSet:=CharSet.Auto)>
        Public Shared Function WNetCancelConnection2(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Boolean) As Integer
        End Function
    
        Private Sub MapNetworkDrive()
            Dim nr As New NETRESOURCE()
            nr.dwType = 1 ' RESOURCETYPE_DISK
            nr.lpRemoteName = "\\192.168.80.111\mansour"
    
            Dim result As Integer = WNetAddConnection2(nr, "00137300", "Almas", 0)
    
            If result <> 0 Then
                Throw New System.ComponentModel.Win32Exception(result)
            End If
        End Sub
    
        Private Sub DisconnectNetworkDrive()
            Dim result As Integer = WNetCancelConnection2("\\192.168.80.111\mansour", 0, True)
            If result <> 0 Then
                Throw New System.ComponentModel.Win32Exception(result)
            End If
        End Sub
    
        Private Sub WriteToFile()
            Try
                MapNetworkDrive()
    
                Dim filePath As String = "\\192.168.80.111\mansour\asd.txt"
                Dim textToWrite As String = "Hello, World!"
    
                System.IO.File.WriteAllText(filePath, textToWrite)
    
            Catch ex As Exception
                Console.WriteLine("Error: " & ex.Message)
            Finally
                DisconnectNetworkDrive()
            End Try
        End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.