2016-03-06 28 views
-1

如果有人可以帮助我,我将非常感激。如果我点击发送按钮,就不能下载的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 

我一切办法,我知道的......类型的“未处理的异常试过“系统.Net.WebException'发生在System.dll中“时,当它尝试下载文件后,我单击发送按钮时引发。

感谢您的帮助提前!

+0

是否'WebException'有任何'InnerException'? – Rhumborl

+0

我现在不在家,但是当我回来时我会看一看。 – paksyh15

+0

它在附加信息中说:“WebClient请求期间发生异常。”我无法得到它写出内部异常.. Prephaps我做错了什么? http://pastebin.com/zxiJjFUH – paksyh15

回答

0

是的,这是不关闭的流。谢谢!

0

如果在流关闭之前您收到异常,代码会直接跳到catch块,并且流永远不会关闭。我假定文件被锁定,直到程序终止。

尝试添加一个命令来关闭文件中的Catch块,以及在你的Try