我想在我拥有的服务器上编辑一个文本文件,但我并不确定。我正在寻找的是这样的:
dim username as string
dim password as string
dim serveraddr as string
username = "MyUsername"
password = "MyPassword"
serveraddr = "MyServer.hosting.com"
ConnectToServer(serveraddr, username, password)
WriteToFile("sample.txt", "Hello World")
像这样的东西。显然我只是把最后两个“方法”放了,所以我猜这些是我正在寻找的东西,除了我不知道它们叫什么。
答案 0 :(得分:-1)
最好的方法是从服务器删除文件并从您的应用程序重写它,如果您想要将信息添加到文件您可以将其下载到本地计算机然后添加信息,从服务器删除它并重新上传
删除功能
Private Function deletefile(ByRef filepathinftp As String, ByRef password As String, ByRef username As String)
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create(filepathinftp), FtpWebRequest)
Try
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.Method = WebRequestMethods.Ftp.DeleteFile
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
ftpResponse = ftp.GetResponse()
ftpResponse.Close()
Catch ex As Exception
End Try
End Function
从Ftp下载的功能
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("File Path In Ftp Example: ftp ://mysite.com/file.txt"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
' This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("username", "password")
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim text As String = reader.ReadToEnd
最后这里是上传功能
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