以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 如何修改文心一言的vb代码,适配狐表可用? (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=189721) |
-- 作者:ouyangfenghuan -- 发布时间:2023/12/20 22:04:00 -- 如何修改文心一言的vb代码,适配狐表可用? 求修改下面代码让狐表可以用 vb Imports System.IO Imports System.Net Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load \' 设置FTP服务器地址、用户名和密码 Dim ftpServer As String = "ftp.example.com" Dim username As String = "your_username" Dim password As String = "your_password" \' 创建FtpWebRequest对象并设置被动模式 Dim request As FtpWebRequest = (FtpWebRequest.Create(ftpServer & "/") As FtpWebRequest) request.Method = WebRequestMethods.Ftp.ListDirectoryDetails request.UsePassive = True request.Credentials = New NetworkCredential(username, password) \' 发送请求并获取响应 Dim response As FtpWebResponse = request.GetResponse() Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) \' 读取响应并显示文件列表 Dim lines() As String = reader.ReadToEnd().Split(vbCrLf) For Each line As String In lines If line.Contains("drwxr-xr-x") Then \' 目录 Console.WriteLine("Directory: " & line) ElseIf line.Contains("-rwxr-xr-x") Then \' 文件 Console.WriteLine("File: " & line) End If Next \' 关闭连接并释放资源 reader.Close() response.Close() End Sub End Class 在上面的代码中,我们首先设置FTP服务器的地址、用户名和密码。然后,我们创建一个FtpWebRequest对象并设置被动模式。接下来,我们发送请求并获取响应。最后,我们读取响应并显示文件列表。注意,在显示文件列表时,我们假设只有目录和文件两种类型。实际上,FTP服务器返回的文件列表可能包含更多信息。你可以根据需要调整代码以处理其他类型的文件信息。
|
-- 作者:有点蓝 -- 发布时间:2023/12/21 8:44:00 -- 比如按钮 \' 设置FTP服务器地址、用户名和密码 Dim ftpServer As String = "ftp.example.com" Dim username As String = "your_username" Dim password As String = "your_password" \' 创建FtpWebRequest对象并设置被动模式 Dim request As System.Net.FtpWebRequest = (System.Net.FtpWebRequest.Create(ftpServer & "/") As System.Net.FtpWebRequest) request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails request.UsePassive = True request.Credentials = New System.Net.NetworkCredential(username, password) \' 发送请求并获取响应 Dim response As System.Net.FtpWebResponse = request.GetResponse() Dim reader As io.StreamReader = New io.StreamReader(response.GetResponseStream()) \' 读取响应并显示文件列表 Dim lines() As String = reader.ReadToEnd().Split(vbCrLf) For Each line As String In lines If line.Contains("drwxr-xr-x") Then \' 目录 Console.WriteLine("Directory: " & line) ElseIf line.Contains("-rwxr-xr-x") Then \' 文件 Console.WriteLine("File: " & line) End If Next \' 关闭连接并释放资源 reader.Close() response.Close() |
-- 作者:ouyangfenghuan -- 发布时间:2023/12/21 9:21:00 -- Dim request As System.Net.FtpWebRequest = (System.Net.FtpWebRequest.Create(ftpServer & "/") As System.Net.FtpWebRequest) 这个As 提示语法错误 |
-- 作者:有点蓝 -- 发布时间:2023/12/21 9:29:00 -- Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpServer & "/") |
-- 作者:ouyangfenghuan -- 发布时间:2023/12/21 10:55:00 -- \'下载ftp上最近1天的新文件 \' 设置FTP服务器地址、用户名和密码 Dim ftpServer As String = "ftp://192.168.1.24" Dim username As String = "anonymous" Dim password As String = "anonymous-guest" \' 获取当前日期,并减去1天 Dim currentDate As DateTime = DateTime.Now.AddDays( - 1) \' 创建FtpWebRequest对象并设置被动模式 Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpServer & "/") \' As System.Net.FtpWebRequest) request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails request.UsePassive = True request.Credentials = New System.Net.NetworkCredential(username, password) \' 发送请求并获取响应 Dim response As System.Net.FtpWebResponse = request.GetResponse() Dim reader As io.StreamReader = New io.StreamReader(response.GetResponseStream()) \' 读取响应并显示文件列表 Dim lines() As String = reader.ReadToEnd().Split(vbCrLf) For Each line As String In lines If line.Contains("drwxr-xr-x") Then \' 目录 Console.WriteLine("Directory: " & line) ElseIf line.Contains("-rwxr-xr-x") Then \' 文件 \' 检查文件修改时间是否在最近1天内 Dim fileTime As DateTime = DateTime.Parse(line.Split(" "c)(1)) If fileTime >= currentDate Then Console.WriteLine("File: " & line) \' 在这里下载文件到本地 \' 假设你要下载的文件名为filename,本地保存路径为localPath Dim filename As String = line.Split(" "c)(0).Split("."c)(0) & line.Split("."c)(1) Dim localPath As String = "C:\\data\\" & filename Using localFileStream As io.FileStream = New io.FileStream(localPath, FileMode.Create) Dim buffer(1024) As Byte Dim bytesRead As Integer = 0 Dim fileInfo As FtpWebRequest = request.GetRequestStream() fileInfo.Method = WebRequestMethods.Ftp.DownloadFile fileInfo.UsePassive = True fileInfo.Credentials = New NetworkCredential(username, password) fileInfo.ContentOffset = 0 fileInfo.WriteTimeout = 200000 \' 设置超时时间,单位为毫秒 While (True) bytesRead = fileInfo.Read(buffer, 0, buffer.Length) If bytesRead = 0 Then Exit While End If localFileStream.Write(buffer, 0, bytesRead) End While fileInfo.Close() End Using End If End If Next \' 关闭连接并释放资源 reader.Close() response.Close() |
-- 作者:ouyangfenghuan -- 发布时间:2023/12/21 10:55:00 -- 这里还提示错误,请老师指点 |
-- 作者:有点蓝 -- 发布时间:2023/12/21 10:58:00 -- 把命名空间补上呀 Using localFileStream As io.FileStream = New io.FileStream(localPath, io.FileMode.Create) 如果不懂什么是命名空间,百度一下 如果不知道是什么样的命名空间,去微软文档里搜索https://learn.microsoft.com/zh-cn/dotnet/api/system.io.filemode?view=net-8.0,或者打开vs查看
|
-- 作者:ouyangfenghuan -- 发布时间:2023/12/21 13:02:00 -- 这些系统的属性真不会改,寸步难行呀,还麻烦检查修改一下红色后面的代码 \'下载ftp上最近1天的新文件 \' 设置FTP服务器地址、用户名和密码 Dim ftpServer As String = "ftp://192.168.1.24" Dim username As String = "anonymous" Dim password As String = "anonymous-guest" \' 获取当前日期,并减去1天 Dim currentDate As DateTime = DateTime.Now.AddDays( - 1) \' 创建FtpWebRequest对象并设置被动模式 Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpServer & "/") \' As System.Net.FtpWebRequest) request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails request.UsePassive = True request.Credentials = New System.Net.NetworkCredential(username, password) \' 发送请求并获取响应 Dim response As System.Net.FtpWebResponse = request.GetResponse() Dim reader As io.StreamReader = New io.StreamReader(response.GetResponseStream()) \' 读取响应并显示文件列表 Dim lines() As String = reader.ReadToEnd().Split(vbCrLf) For Each line As String In lines If line.Contains("drwxr-xr-x") Then \' 目录 Console.WriteLine("Directory: " & line) ElseIf line.Contains("-rwxr-xr-x") Then \' 文件 \' 检查文件修改时间是否在最近1天内 Dim fileTime As DateTime = DateTime.Parse(line.Split(" "c)(1)) If fileTime >= currentDate Then Console.WriteLine("File: " & line) \' 在这里下载文件到本地 \' 假设你要下载的文件名为filename,本地保存路径为localPath Dim filename As String = line.Split(" "c)(0).Split("."c)(0) & line.Split("."c)(1) Dim localPath As String = "C:\\data\\" & filename Using localFileStream As io.FileStream = New io.FileStream(localPath, io.FileMode.Create) Dim buffer(1024) As Byte Dim bytesRead As Integer = 0 Dim fileInfo As FtpWebRequest = request.GetRequestStream() fileInfo.Method = WebRequestMethods.Ftp.DownloadFile fileInfo.UsePassive = True fileInfo.Credentials = New NetworkCredential(username, password) fileInfo.ContentOffset = 0 fileInfo.WriteTimeout = 200000 \' 设置超时时间,单位为毫秒 While (True) bytesRead = fileInfo.Read(buffer, 0, buffer.Length) If bytesRead = 0 Then Exit While End If localFileStream.Write(buffer, 0, bytesRead) End While fileInfo.Close() End Using End If End If Next \' 关闭连接并释放资源 reader.Close() response.Close()
|
-- 作者:有点蓝 -- 发布时间:2023/12/21 13:30:00 -- 看前面用到FtpWebRequest 的地方是怎么更改的。使用第三方sdk这些都是必要的知识,不懂肯定是寸步难行,不要期望每一个都等着别人改 |
-- 作者:ouyangfenghuan -- 发布时间:2023/12/21 19:39:00 -- 提示错误如图 此主题相关图片如下:提问-_20231221193734.png |