Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我需要创建一个文本文件并将其发送到ftp服务器_.net_Vb.net_Ftp - Fatal编程技术网

.net 我需要创建一个文本文件并将其发送到ftp服务器

.net 我需要创建一个文本文件并将其发送到ftp服务器,.net,vb.net,ftp,.net,Vb.net,Ftp,我目前正在开发一个程序,您可以使用验证码登录帐户。我需要帮助将带有数字字符串的文件发送到我拥有的服务器 例如: 将“thisfile.txt”发送到ftp://myserver.com/thisfile.txt 我现在有 Imports System.IO.File Public value = rnd.Next(1000, 9999) Public vcode As Int32 = rndvalue File.CreateText("vcode.vlo") .Net有一个内置的,您可以使用它

我目前正在开发一个程序,您可以使用验证码登录帐户。我需要帮助将带有数字字符串的文件发送到我拥有的服务器

例如:

将“thisfile.txt”发送到
ftp://myserver.com/thisfile.txt

我现在有

Imports System.IO.File
Public value = rnd.Next(1000, 9999)
Public vcode As Int32 = rndvalue
File.CreateText("vcode.vlo")
.Net有一个内置的,您可以使用它通过FTP上传文件

MSDN包含一个有用的示例,它是C语言,但很容易转换为VB.Net:

Sub Main()
    ' Get the object used to communicate with the server.
    Dim request = CType(WebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.UploadFile

    ' This example assumes the FTP site uses anonymous logon.
    request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")

    ' Copy the contents of the file to the request stream.
    Dim sourceStream = New StreamReader("testfile.txt")
    Dim fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
    sourceStream.Close()
    request.ContentLength = fileContents.Length

    Dim requestStream = request.GetRequestStream()
    requestStream.Write(fileContents, 0, fileContents.Length)
    requestStream.Close()

    Dim response = CType(request.GetResponse(), FtpWebResponse)

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)

    response.Close()
End Sub
.Net有一个内置的,您可以使用它通过FTP上传文件

MSDN包含一个有用的示例,它是C语言,但很容易转换为VB.Net:

Sub Main()
    ' Get the object used to communicate with the server.
    Dim request = CType(WebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.UploadFile

    ' This example assumes the FTP site uses anonymous logon.
    request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")

    ' Copy the contents of the file to the request stream.
    Dim sourceStream = New StreamReader("testfile.txt")
    Dim fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
    sourceStream.Close()
    request.ContentLength = fileContents.Length

    Dim requestStream = request.GetRequestStream()
    requestStream.Write(fileContents, 0, fileContents.Length)
    requestStream.Close()

    Dim response = CType(request.GetResponse(), FtpWebResponse)

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)

    response.Close()
End Sub

这里有一个将任何文件上传到ftp服务器的功能

Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)

' Create FtpWebRequest object from the Uri provided
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)

' Provide the WebPermission Credintials
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)

' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_FtpWebRequest.KeepAlive = False

' set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000

' Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

' Specify the data transfer type.
_FtpWebRequest.UseBinary = True

' Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length

' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte

' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

Try
    ' Stream to which the file to be upload is written
    Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

    ' Read from the file stream 2kb at a time
    Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

    ' Till Stream content ends
    Do While contentLen <> 0
        ' Write Content from the file stream to the FTP Upload Stream
        _Stream.Write(buff, 0, contentLen)
        contentLen = _FileStream.Read(buff, 0, buffLength)
    Loop

    ' Close the file stream and the Request Stream
    _Stream.Close()
    _Stream.Dispose()
    _FileStream.Close()
    _FileStream.Dispose()
Catch ex As Exception
    MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

这里有一个将任何文件上传到ftp服务器的功能

Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)

' Create FtpWebRequest object from the Uri provided
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)

' Provide the WebPermission Credintials
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)

' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_FtpWebRequest.KeepAlive = False

' set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000

' Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

' Specify the data transfer type.
_FtpWebRequest.UseBinary = True

' Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length

' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte

' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

Try
    ' Stream to which the file to be upload is written
    Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

    ' Read from the file stream 2kb at a time
    Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

    ' Till Stream content ends
    Do While contentLen <> 0
        ' Write Content from the file stream to the FTP Upload Stream
        _Stream.Write(buff, 0, contentLen)
        contentLen = _FileStream.Read(buff, 0, buffLength)
    Loop

    ' Close the file stream and the Request Stream
    _Stream.Close()
    _Stream.Dispose()
    _FileStream.Close()
    _FileStream.Dispose()
Catch ex As Exception
    MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub