ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Check Gmail using C#

<< < (6/8) > >>

kyrathaba:
Used this FTP code, but got the error shown below:


--- Code: C# ---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);            }

kyrathaba:
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.


--- Code: C# ---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();        }       }

wraith808:
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 :)

kyrathaba:
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?

f0dder:
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 :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version