简体   繁体   中英

How to delete all text in a text file on ftp server without deleting file itself in vb.net

Hi I am doing a chat orientated program in vb.net. I have everything working nicely but there is a feature I am trying to add where if the user types /clear it will then delete the chat text from the file, (the chat is an ftp connection and it writes to a text file)

i'm hoping to only delete text from the file not the whole file itself, i have tried appending but that as you probably know already just writes extra text to the file.

If anyone can help it would be great cheers :D

The way to go should be upload a new empty file overwriting the one on the FTP, with the same filename and path.

Dim wcFTP As New WebClient()

'wcFTP.Proxy = ...
wcFTP.BaseAddress = "ftp://foobar.domain.com"
wcFTP.Credentials = New NetworkCredential("user", "pass") 

Dim sFilePath As String '= ...
Dim sFtpPath As String = wcFTP.BaseAddress & sFolder & Path.GetFileName(sFilePath)

wcFTP.UploadFile(sFtpPath, sFilePath)

Where sFilePath is the path of the empty file with the same name as the one in the server.


To get an empty file you can donwload and empty the file on the ftp, or simply create a new file with the same name and no data. For example:

Dim sFilePath As String = "C:\Folder\fileName.xml"
File.Create(sFilePath).Dispose()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM