Hello to all,
i used the above code and i am happy to inform you that it works. But i have a problem. I want to make an application that uploads screenshots of my desktop to my server via ftp.
This is the code i use:
When i run the application and press button1 the image of my desktop is being saved to c:\desktop2.jpg and is being uploaded to the server. But when i click the button for second time (or third, or....) this error is produced at line "clsStream = clsRequest.GetRequestStream()":
Public Class Form1
Dim SC As New ScreenShot.ScreenCapture
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("*"), System.Net.FtpWebRequest)
Dim bFile() As Byte
Dim clsStream As System.IO.Stream
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
clsRequest.Credentials = New System.Net.NetworkCredential("*", "*")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bFile = System.IO.File.ReadAllBytes("c:\desktop2.jpg")
clsStream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
"Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress."
So i tried to make a modification and i put "clsStream = clsRequest.GetRequestStream()" out of the button1_Click() and paste it at form1_Load().
When i try to run this, it works for the first button1 click but at the second a new error comes at line "clsStream.Write(bFile, 0, bFile.Length)": Cannot access a disposed object.
So i decided to delete the two lines that close and dispose the object (clsStream.Close(), clsStream.Dispose()). I run this again, first click everything is right, second click no error produced but although the new desktop image is being saved to c:, the file i found to my server is the old one but (almost) double in size. If i press for third time the button, the local image is refreshed but the image on server is the old one but (almost) with triple size of the original.
What's the problem? I think that i want to do an easy job but i can't. Just press the button upload an image via ftp to server. Press again the button, upload an image to server and so on.
Thank in advance,