用Inet1做了个在线更新程序,但有个问题一直搞不好,现500分求解

52694 2003-06-14 11:14:40
就是把要更新的文件路径存在数据库里,然后读出来一个一个的更新。
就是利用循环更新多个文件,但老出错。何故?

附源代码:



Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long '对文件的操作指令
pFrom As String '源文件或路径
pTo As String '目的文件或路径
fFlags As Integer '操作标志
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type

Private Declare Function SHFileOperation Lib _
"shell32" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function GetWindowsDirectory _
Lib "kernel32" Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As _
Long) As Long

Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Const FOF_ALLOWUNDO = &H40

Dim DirString As String

'-------------------------

Private m_lngDocSize As Long '该变量用于存储Web页面文件大小

'Private Const strURL = "http://www.wupanwang.com/zip/wupan2000.exe"
'Private Const strURL = "http://www.hlyex.com/csd.exe"
'Private Const FileName = "wupan2000.exe"
'Private Const FileName = "csd.exe"
'------------------------------------
Private strurl As String
Private filename As String

Private Sub Command1_Click()
Call gt
End Sub

Private Sub gt()
'用FOR 循环做试验
For y = 1 To 5
strurl = "http://www.wupanwang.com/zip/wupan2000.exe"
filename = y & wupan2000.exe
'文件大小值复位
m_lngDocSize = 0

'复位进度条控件
ProgressBar1.Value = 0.001

'显示进度的标签内容设为空
lblProgressInfo.Caption = ""

'定义ITC控件使用的协议为HTTP协议
Inet1.Protocol = icHTTP




'调用Execute方法向Web服务器发送HTTP请求
Inet1.Execute Trim$(strurl), "GET"
lblProgressInfo.Caption = "请等待..."
Next

End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Shell App.Path & "\" & filename, vbNormalFocus
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)

Dim binBuffer() As Byte
Dim sngProgerssValue As Single
Dim iBlock As Long

'On Error Resume Next
iBlock = 0
On Error Resume Next
Kill App.Path & "\" & filename

Select Case State

Case icResponseCompleted
'打开文件供写入

Open App.Path & "\" & filename For Binary Access Write As #1

Do '从缓冲区读取数据
DoEvents

binBuffer = Inet1.GetChunk(512, icByteArray)
'strText = strText & strBuffer
iBlock = iBlock + 1
If m_lngDocSize > 0 Then
'获得进度百分比值
sngProgerssValue = Int((iBlock * 512 / m_lngDocSize) * 100)
'更新进度标签显示内容
lblProgressInfo.Caption = "已下载 " & CStr(iBlock * 512) & " 字节 (" & CStr(sngProgerssValue) & "%)"
'用新值更新进度条控件
ProgressBar1.Value = sngProgerssValue
'写入文件
Put #1, , binBuffer()
End If

Loop Until iBlock * 512 >= m_lngDocSize
'关闭文件
Close #1
MsgBox "升级完成", vbOKOnly Or vbInformation, "在线升级"

'Shell App.Path & "\" & filename
'Call gt

Case icResponseReceived

If m_lngDocSize = 0 Then

'读取页面文件大小
If Len(Inet1.GetHeader("Content-Length")) > 0 Then
m_lngDocSize = CLng(Inet1.GetHeader("Content-Length"))
If (m_lngDocSize = 0) Then
MsgBox "读取远程数据出错", vbOKOnly Or vbExclamation, "在线升级"
End If

Else
MsgBox "ERROR!", vbOKOnly Or vbExclamation, "在线升级"
End If

End If

Case icError
MsgBox "与主机通信出错", vbOKOnly Or vbExclamation, "在线升级"

Case icResolvingHost
lblProgressInfo.Caption = "正在查找主机..."

Case icHostResolved
lblProgressInfo.Caption = "已经找到主机"

Case icConnecting
lblProgressInfo.Caption = "正在联系主机"

Case icConnected
lblProgressInfo.Caption = "已经连接到主机"

Case icRequesting
lblProgressInfo.Caption = "正在发送请求..."

Case icRequestSent
lblProgressInfo.Caption = "成功发送请求"

Case icReceivingResponse
lblProgressInfo.Caption = "正在接收回应..."

Case icDisconnecting
lblProgressInfo.Caption = "正在断开连接..."

Case icDisconnected
lblProgressInfo.Caption = "已经断开连接"

End Select

End Sub
...全文
133 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
削死壹狼 2003-06-16
  • 打赏
  • 举报
回复
借兄弟宝地一用 谢啦!!
---------------------------------------------------
如何能让我的程序运行后 在用户又切换到别的窗口进行操作时 到了我的程序内部规定的条件 能自动跳出 屏蔽用户当前其它操作 直到给我的程序一个响应

不知我说的是否清楚 请大家帮帮我 谢谢!
sxs69 2003-06-16
  • 打赏
  • 举报
回复
出现什么问题呢?
veisa 2003-06-16
  • 打赏
  • 举报
回复
学习+UP
sxs69 2003-06-15
  • 打赏
  • 举报
回复
Shell App.Path & "\" & filename
如果下载ini,bat,rar等文件那么用SHELL是打不开的应该用ShellExecute

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

ShellExecute 0, vbNullString, App.Path & "\" & FileName, "", "", vbNormalFocus
52694 2003-06-15
  • 打赏
  • 举报
回复
sxs69() :

还有一个问题就是,好象只能下载EXE文件,下载其它文件时好象出问题。(如:ini,bat,rar等文件都不行)
52694 2003-06-15
  • 打赏
  • 举报
回复
sxs69()

我有点不明啊
能说详细点吗?
或给个例子啊

谢谢你了
kmzs 2003-06-15
  • 打赏
  • 举报
回复
注意:
从缓冲区读取时需要将读取的数据的前12个bytes截掉,否则拼在一起一定不对……
--------------------------------------------------------------------------
kmandzs@hotmail.com
sxs69 2003-06-14
  • 打赏
  • 举报
回复
我试了三次,每次都能成功啊,不过不能用FOR 循环做试验,把for y=1 to 5去掉
另外
filename = y & wupan2000.exe
改为
filename = "wupan2000.exe"
zmrok 2003-06-14
  • 打赏
  • 举报
回复
我看不明白,帮你顶一把
sxs69 2003-06-14
  • 打赏
  • 举报
回复
原来如此,能不能这样,你定义一个变量每次下载完后置为真
Dim DirString As String
Dim intEnd As Boolean

For y = 1 To 5
intEnd = False '先置为FALSE
strurl = "http://www.wupanwang.com/zip/wupan2000.exe"
filename = Format(y) & "wupan2000.exe"
' filename = Format(1) & "wupan2000.exe"
'文件大小值复位
m_lngDocSize = 0

'复位进度条控件
ProgressBar1.Value = 0.001

'显示进度的标签内容设为空
lblProgressInfo.Caption = ""

'定义ITC控件使用的协议为HTTP协议
Inet1.Protocol = icHTTP
'调用Execute方法向Web服务器发送HTTP请求
Inet1.Execute Trim$(strurl), "GET"
lblProgressInfo.Caption = "请等待..."

Do While intEnd = False '等待上一个文件下载完
DoEvents
Loop
Next

End Sub

'这里加上
intEnd = True
MsgBox "升级完成", vbOKOnly Or vbInformation, "在线升级"


我实验正好完成,已经通过了
52694 2003-06-14
  • 打赏
  • 举报
回复
sxs69() :

如果去掉for那不是下载单一个文件了?
我要的是下载多个文件啊

filename = y & wupan2000.exe
应该是:
filename = y & "wupan2000.exe"
这样的,这样区分试验中下载多个文件

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧