如果有人可以帮助我,我真的很感激。如果我点击发送按钮,则无法在此之后下载log.txt ...
Imports System
Imports System.IO
Imports System.Collections
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Imports System.Net
Public Class Form1
Public sent As Boolean = False
Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Int32, _
ByVal uFlags As Int32, _
ByVal dwitem1 As Int32, _
ByVal deitem2 As Int32) As Int32
Private Sub send_Click(sender As Object, e As EventArgs) Handles send.Click
'If a message has been sent
sent = True
Try
Dim writer As New System.IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
sent = False
writer.Write(input.Text)
Try
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://***/c/log.txt"), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.DeleteFile
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
ftpResponse = ftp.GetResponse()
ftpResponse.Close()
Catch ex As Exception
End Try
My.Computer.Network.UploadFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", New Uri("ftp://***/c/log.txt"), "user", "pass", False, 30000, FileIO.UICancelOption.DoNothing)
input.Text = ""
Catch ex As Exception
End Try
sent = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c") Then
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c")
Else
My.Computer.FileSystem.DeleteDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c", FileIO.DeleteDirectoryOption.DeleteAllContents)
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c")
End If
firstcheck()
Threading.Thread.Sleep(500)
End Sub
Private Sub taskbarcheck_Tick(sender As Object, e As EventArgs) Handles taskbarcheck.Tick
If File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt") Then
Me.Opacity = 100
Me.Show()
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt")
Catch ex As Exception
End Try
Threading.Thread.Sleep(50)
SHChangeNotify(&H8000000, &H1000, 0, 0)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_hide.Click
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\chax.txt")
Catch ex As Exception
End Try
Me.Opacity = 0
Me.Hide()
Threading.Thread.Sleep(50)
SHChangeNotify(&H8000000, &H1000, 0, 0)
End Sub
Private Sub ftpstuff_Tick(sender As Object, e As EventArgs) Handles ftpstuff.Tick
'Try
'delete file if exists
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
Catch ex As Exception
End Try
'download file
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
Catch ex As Exception
End Try
'My.Computer.Network.DownloadFile("http://***/c/log.txt", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", "", "", False, 30000, False)
Dim myWebClient As New WebClient()
If Not sent = True Then
myWebClient.DownloadFile("http://***/c/log.txt", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
log.Text = File.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt")
End If
End Sub
Private Sub firstcheck()
If Not File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt") Then
My.Computer.Network.DownloadFile(New Uri("ftp://***/c/log.txt"), My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\c\log.txt", "user", "pass", False, 30000, True, FileIO.UICancelOption.DoNothing)
End If
End Sub
我按照我所知道的方式尝试了一下......当我点击发送按钮后尝试下载文件时,会抛出“System.dll中出现未处理的类型'System.Net.WebException'的异常”
提前感谢您的帮助!
答案 0 :(得分:0)
是的,这是没有关闭的流。谢谢!
答案 1 :(得分:0)
如果在流关闭之前出现异常,则代码会直接跳转到catch块,并且流永远不会关闭。我假设文件被锁定,直到程序终止。
尝试添加命令以关闭Catch
块以及Try
块中的文件