|
kyrathaba
|
 |
« Reply #25 on: December 04, 2010, 09:58:39 AM » |
|
Used this FTP code, but got the error shown below: Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] string FTPAddress = "ftp://glensforkumc.com/non_church"; FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/UploadDownload.pdb"); request.Method = WebRequestMethods.Ftp.UploadFile; request .Credentials = new NetworkCredential (username, password ); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; FileStream stream = File.OpenRead("UploadDownload.pdb"); byte[] buffer = new byte[stream .Length]; stream.Read(buffer, 0, buffer.Length); stream.Close(); try { Stream reqStream = request.GetRequestStream(); reqStream.Write(buffer, 0, buffer.Length); reqStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); }

|
|
|
|
« Last Edit: December 04, 2010, 10:39:56 AM by kyrathaba »
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #26 on: December 04, 2010, 08:10:22 PM » |
|
I spent some time this afternoon experimenting. Hacked out this code that seems to work (see class code-listing below). Note: allows uploading to specific directory on your website, downloading from a specific directory to a specific local directory, and has ability to return a list of files found in the root of any directory. I also posted a copy of the code on my DC C# Errata. Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] public class FTPClient { // The hostname or IP address of the FTP server private string _remoteHost; // The remote username private string _remoteUser; // Password for the remote user private string _remotePass; public FTPClient(string remoteHost, string remoteUser, string remotePassword) { _remoteHost = remoteHost; _remoteUser = remoteUser; _remotePass = remotePassword; } public string DirectoryListing() { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost); request.Method = WebRequestMethods.Ftp.ListDirectory; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader (responseStream ); string result = string.Empty; while (!reader.EndOfStream) { result += reader.ReadLine() + Environment.NewLine; } reader.Close(); response.Close(); return result; } public string DirectoryListing(string folder) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + folder); request.Method = WebRequestMethods.Ftp.ListDirectory; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader (responseStream ); string result = string.Empty; while (!reader.EndOfStream) { result += reader.ReadLine() + Environment.NewLine; } reader.Close(); response.Close(); return result; } public void Download(string file, string destination) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + file); request.Method = WebRequestMethods.Ftp.DownloadFile; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader (responseStream ); StreamWriter writer = new StreamWriter (destination ); writer.Write(reader.ReadToEnd()); writer.Close(); reader.Close(); response.Close(); } public void UploadFile(string FullPathFilename) { string filename = Path.GetFileName(FullPathFilename); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename); request.Method = WebRequestMethods.Ftp.UploadFile; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); StreamReader sourceStream = new StreamReader (FullPathFilename ); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); requestStream.Close(); sourceStream.Close(); } }
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
wraith808
|
 |
« Reply #27 on: December 06, 2010, 08:52:57 AM » |
|
So, that still doesn't explain why you can't use ftp? Or is it just that you don't want to? UPDATE: nevermind... didn't see your last posts 
|
|
|
|
|
Logged
|
|
|
|
|
|
kyrathaba
|
 |
« Reply #28 on: December 26, 2010, 02:35:17 PM » |
|
RESOLVED (see my next post). An interesting issue has arisen in a project I'm working on: I've created the "C:/[User]/AppData/Roaming/Kyrathaba/ExcuseManager/" subdirectory programmatically, using: Directory.CreateDirectory() So far, so good. But then, using my Imap code, I try to download email attachments to the above directory, and I get an Unauthorized Access Expression. Funny, because it just let me create the directory, but it now won't let me download anything to it. The code referenced by the link above works, if you just create a default Windows Forms project in C#. Must have something to do with the fact that I'm trying to write a binary file into a subdirectory of /[User]/; must be upsetting UAC or something, because I am able to write simple text files to the directory. Any ideas what's going on?
|
|
|
|
« Last Edit: December 26, 2010, 02:54:07 PM by kyrathaba »
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
f0dder
|
 |
« Reply #29 on: December 26, 2010, 02:52:51 PM » |
|
Hmm. 1) how are you creating the above folder? 2) what kind of app is it, and how is it deployed? I haven't had to deal with this myself yet, but I guess you might have to look at the SecurityPermissionAttribute stuff. Hopefully there's some help around here 
|
|
|
|
|
Logged
|
 - carpe noctem
|
|
|
|
kyrathaba
|
 |
« Reply #30 on: December 26, 2010, 02:53:25 PM » |
|
Figured it out: I wasn't adding a filename and extension to the end of the filename I tried to build. DUH and DOUBLE-DUH!
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #31 on: December 26, 2010, 02:59:06 PM » |
|
@fodder: Thanks for being willing to take time and try to help. Turns out there isn't a problem, except me being inattentive.
If you can programmatically create a subdirectory, you can write a file there, as you'd expect.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
f0dder
|
 |
« Reply #32 on: December 26, 2010, 03:03:59 PM » |
|
Turns out there isn't a problem, except me being inattentive. Bugs happen ^_^ If you can programmatically create a subdirectory, you can write a file there, as you'd expect. Well, it's definitely what you'd expect - I was guessing that perhaps the IMAP library was running with more paranoid security settings or something. I should really find the time to read properly about .NET security.
|
|
|
|
|
Logged
|
 - carpe noctem
|
|
|
|
kyrathaba
|
 |
« Reply #33 on: January 01, 2011, 02:21:39 PM » |
|
I have the following functioning code to upload programmatically via FTP in C#: Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] public void UploadFile(string FullPathFilename) { string filename = Path.GetFileName(FullPathFilename); try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename); request.Method = WebRequestMethods.Ftp.UploadFile; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); StreamReader sourceStream = new StreamReader (FullPathFilename ); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); requestStream.Close(); sourceStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Upload error"); } finally { }
Now for the problem. When I use this method in the following button click event, it does upload the file to the desired directory of my website, but then my UI hangs. I have to kill the program from within the IDE. Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] private void btnUploadTxtFile_Click(object sender, EventArgs e) { string username = "my_username"; string password = "my_password"; string host = "ftp://glensforkumc.com"; try { clsFTPclient client = new clsFTPclient (host + "/httpdocs/non_church/", username, password ); client.UploadFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\myTextFile.txt"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Upload problem"); } }
Doesn't hang when using a similar method to download a file. Any idea why my UI is freezing up after a successful upload?
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
f0dder
|
 |
« Reply #34 on: January 01, 2011, 02:40:54 PM » |
|
Have you tried tracing the UploadFile method in the VS debugger to see if there's any particular line of code that causes a hang? It's not a good idea doing stuff like this from your UI thread, anyway... you should either schedule the task on a background worker thread, or (better) use Async I/O. I'll refrain from saying anything about having non-ui code in a btnWhatever_Click handler 
|
|
|
|
|
Logged
|
 - carpe noctem
|
|
|
|
kyrathaba
|
 |
« Reply #35 on: January 01, 2011, 02:49:49 PM » |
|
Good points, all. I was just doing a quick and dirty test of the UploadFile method is all. In a full-blown application, I'd have the btnWhatever_Click handler hand off the task to a backgroundWorker object.
Maybe I'll create one and see if that fixes this issue. Although, in the current case, why does DownloadFile work without hanging the UI, but UploadFile hangs it? Hmm...
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #36 on: January 01, 2011, 03:09:34 PM » |
|
Okay, my button Click event now hands the file upload off to a backgroundWorker object running on its own thread: Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { MessageBox.Show("Doing some work..."); string username = "my_username"; string password = "my_password"; string host = "ftp://glensforkumc.com"; string myLocal = Path.GetDirectoryName(Application.ExecutablePath) + "\\myTextFile.txt"; string myRemote = host + "/httpdocs/non_church/"; clsFTPclient client = new clsFTPclient (host + "/httpdocs/non_church/", username, password ); client.UploadFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\myTextFile.txt"); } private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { MessageBox.Show("I'm done uploading"); }
The file does get uploaded, but my RunWorkerCompleted() messageBox never pops up... remove everything except the messageBox from the _DoWork() method above, and it's msgBox (as well as RunWorkerCompleted's msgBox) both fire as expected.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #37 on: January 01, 2011, 04:26:11 PM » |
|
Interesting. The second MessageBox never gets invoked. At least, I never see it pop up when I run the program click my button to upload the file: Formatted for C# with the GeSHI Syntax Highlighter [ copy or print] public void UploadFile(string FullPathFilename) { string filename = Path.GetFileName(FullPathFilename); try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename); request.Method = WebRequestMethods.Ftp.UploadFile; request .Credentials = new NetworkCredential (_remoteUser, _remotePass ); StreamReader sourceStream = new StreamReader (FullPathFilename ); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); MessageBox.Show("About to make upload request..."); //this one "pops-up", as expected FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //does get invoked (my file DOES get uploaded) MessageBox.Show("Made upload request. About to close streams..."); //I never see this one response.Close(); requestStream.Close(); sourceStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Upload error"); } finally { } }
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #38 on: January 01, 2011, 04:48:18 PM » |
|
I'll be scratching my head over this one for a while, but it all works as expected now; no inexplicable hanging (even when doing the upload on its own asynch thread, it had 'seemed' to be hanging, in that I got a non-responsive UI):
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|
kyrathaba
|
 |
« Reply #39 on: January 01, 2011, 07:10:48 PM » |
|
I've updated my Snipplr posting here.
|
|
|
|
|
Logged
|
Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 20.0 _________________________________________________________________________________________ I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF http://kyrathaba.dcmembers.com/donate.htm
|
|
|
|