none
Copy/Send File To Other Shared Drive With Username & Password RRS feed

  • Question

  • Hi. I need to copy or send my files to other shared drive such as drive S://. But, it contains a username and password. 

    I've been search this method and if I am not mistaken, this is called network mapping. 

    I try to understand the flow of the codes, but I not really understand how it works. To be honest, I am a beginner to this programming and have a long way to learn. Please help me to understand the flow to do this problem. 

    What should I do for the first step? 

    Or maybe there are any free open source that can do this, copy file to other network?

    I would like to share the code, but I don't know what code and the method. 

    Help me and Thank you a lot.

    Thursday, June 27, 2019 12:24 AM

Answers

  • Hi,

    I made a demo,you can try it:

    Imports System.IO
    Imports System.Net
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            UpLoadFile("D:\test.txt", "\\WS-xxx-xxxx\Public", "xxx", "xxx")
        End Sub
        Public Sub UpLoadFile(ByVal fileNamePath As String, ByVal urlPath As String, ByVal User As String, ByVal Pwd As String)
            Dim newFileName As String = fileNamePath.Substring(fileNamePath.LastIndexOf("\") + 1)
            If urlPath.EndsWith("\") = False Then urlPath = urlPath & "\"
            urlPath = urlPath & newFileName
            Dim myWebClient As WebClient = New WebClient()
            Dim cread As NetworkCredential = New NetworkCredential(User, Pwd, "Domain")
            myWebClient.Credentials = cread
            Dim fs As FileStream = New FileStream(fileNamePath, FileMode.Open, FileAccess.Read)
            Dim r As BinaryReader = New BinaryReader(fs)
    
            Try
                Dim postArray As Byte() = r.ReadBytes(CInt(fs.Length))
                Dim postStream As Stream = myWebClient.OpenWrite(urlPath)
    
                If postStream.CanWrite Then
                    postStream.Write(postArray, 0, postArray.Length)
                    MessageBox.Show("File upload succeeded", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("File upload failed", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End If
    
                postStream.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "error")
            End Try
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Marked as answer by stizalke Thursday, June 27, 2019 2:32 AM
    • Edited by Alex Li-MSFT Thursday, June 27, 2019 2:37 AM
    Thursday, June 27, 2019 1:36 AM

All replies

  • Hi,

    I made a demo,you can try it:

    Imports System.IO
    Imports System.Net
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            UpLoadFile("D:\test.txt", "\\WS-xxx-xxxx\Public", "xxx", "xxx")
        End Sub
        Public Sub UpLoadFile(ByVal fileNamePath As String, ByVal urlPath As String, ByVal User As String, ByVal Pwd As String)
            Dim newFileName As String = fileNamePath.Substring(fileNamePath.LastIndexOf("\") + 1)
            If urlPath.EndsWith("\") = False Then urlPath = urlPath & "\"
            urlPath = urlPath & newFileName
            Dim myWebClient As WebClient = New WebClient()
            Dim cread As NetworkCredential = New NetworkCredential(User, Pwd, "Domain")
            myWebClient.Credentials = cread
            Dim fs As FileStream = New FileStream(fileNamePath, FileMode.Open, FileAccess.Read)
            Dim r As BinaryReader = New BinaryReader(fs)
    
            Try
                Dim postArray As Byte() = r.ReadBytes(CInt(fs.Length))
                Dim postStream As Stream = myWebClient.OpenWrite(urlPath)
    
                If postStream.CanWrite Then
                    postStream.Write(postArray, 0, postArray.Length)
                    MessageBox.Show("File upload succeeded", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("File upload failed", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End If
    
                postStream.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "error")
            End Try
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Marked as answer by stizalke Thursday, June 27, 2019 2:32 AM
    • Edited by Alex Li-MSFT Thursday, June 27, 2019 2:37 AM
    Thursday, June 27, 2019 1:36 AM
  • Hi. Thank you Alex. The code is simple and easy to understand, plus, it also work. I don't know how to pay you. May God Bless your family and you!
    Thursday, June 27, 2019 2:34 AM