Page 1 of 1

[Answered] Reading from a FTP Server

Posted: Tue Sep 22, 2009 2:29 am
by Click16
I want to know how Troy got that entity updater working. I downloaded the source, but I don't understand it. It would be a lot easier if it was labeled. :¬_¬: But anyway, If anyone wants to help out, it will be greatly appreciated.

Re: Reading from a FTP Server

Posted: Tue Sep 22, 2009 2:36 am
by Twinreaper
Not sure what you mean? Would you want me to make something on the FTP's server labeled as new builds?

Re: Reading from a FTP Server

Posted: Tue Sep 22, 2009 2:55 am
by Click16
I just want to know how to read / write a file on a ftp server. like lets say i have a text file on my ftp folder here on remnant, i want an application to be able to read and write to that file.

Re: Reading from a FTP Server

Posted: Tue Sep 22, 2009 3:35 am
by troymac1ure
Click16 wrote:I want to know how Troy got that entity updater working. I downloaded the source, but I don't understand it. It would be a lot easier if it was labeled. :¬_¬: But anyway, If anyone wants to help out, it will be greatly appreciated.
Sorry if it's not as documented as you'd like. I had no idea what I was doing when I wrote this. It was all by trial and error (as with most of Entity's add-ons) as I have only been working with c# for about a year now.

The updater code is compilicated, but if you just want to read a text file, do a search through the code (main.cs) for the .NFO file. I'm not at my laptop right now, so I can't remember too much of it. It's something like .ReadString()

It reads the .NFO file to get the version and release date and other info and compares it to the current version. If it needs the update, THEN it calls the UpdateEnt.EXE file. So the code inside the MAIN.CS file should be very straight forward.

EDIT: (Just d/l the source, here's the code for reading a text file)

Code: Select all

            // Used for HTTP/FTP access
            WebClient wc = new WebClient();

            // Set our FTP Username & Password
            wc.Credentials = new System.Net.NetworkCredential(updateFTPName, updateFTPPass);

            int count = 0;  // used for retries if we fail
            string[] textFileData = null;  // each line of the file saved as a string
            while (textFileData == null)
            {
                try
                {
                    // FTPServer Path (ending in \) and textFile (name)
                    textFileData = wc.DownloadString(FTPServer + textFile).Split('\n');
                }
                catch (WebException e)
                {
                    // Give 5 chances to connect and d/l file
                    count++;
                    if (count < 5)
                        Thread.Sleep(300);  // allow 300ms between tries
                    else
                        throw e;  // after 5 chances, throw an exception
                }
            }       
            wc.Dispose();
See? quite straight forward.

Re: Reading from a FTP Server

Posted: Wed Sep 23, 2009 12:02 am
by OwnZ joO
I'll try to post another way of doing it sometime by using the FtpWebRequest class if you want to see that.

Re: Reading from a FTP Server

Posted: Wed Sep 23, 2009 1:01 am
by Click16
Its ok. I got everything working. keep this post for reference.

Re: [Answered] Reading from a FTP Server

Posted: Thu Jan 28, 2010 11:41 am
by DrXThirst
I would like to post the code I wrote before I even saw this thread for an automatic updater if I have the time later. Maybe it can help you out some for future applications you build. :)

EDIT: More than a month later, I present you with my "Check for Updates" code.
The following will get the file extension of any URL. (Thanks to Detox, obviously.)

Code: Select all

public string GetFileExtension(string URL)
        {
            //Thanks to -DeToX-
            string[] tmpString = URL.Split('/');
            string[] tmpString2 = tmpString[tmpString.Length - 1].Split('.');
            return tmpString2[tmpString2.Length - 1];
        }
The next code is actually checking for an update... I will comment:

Code: Select all

public void startupcheckforupdates()
        {
            //Start a new xml document reader
            XmlDocument doc = new XmlDocument();
            //Load the document from a URL
            doc.Load("http://www.gagefaulkner.com/programs/GTFO/version.xml");
            //Make the reader get the document elements
            XmlElement root = doc.DocumentElement;
            //Read the nodes in the XML document
            XmlNodeList nodes = root.SelectNodes("/xml");
            //Load the nodes.
            foreach (XmlNode node in nodes)
            {
                //Refer to the "version" node as "name."
                string name = node["version"].InnerText;
                //An invisible label in the program now loads the version number onto it.
                verlabel.Text = name;
            }

            //If that version label is the same as the newest version of the program, then,
            if (verlabel.Text == "1.0.0")
            {
                //Do nothing
                return;
            }

            //If it's not,
            else
            {
                //Show a messagebox telling what version you have and what version has been released.
                MessageBox.Show("You have " + currentversion.Text + "." + Environment.NewLine + "Version " + verlabel.Text + " has been released." + Environment.NewLine + "Please download the new update!", "A New Update Has Been Released!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //Make the string "URL" refer to the URL of the newest version of the program
                string URL = "http://www.gagefaulkner.com/programs/GTFO/Gamertag Follower.zip";
                //Make the string "Extension" get the file extension of the "URL" string.
                string Extension = GetFileExtension(URL);
                //Make the string "FName" equal the "FileName" to put in the "FileName" textbox.
                string FName = Path.GetFileNameWithoutExtension("http://www.gagefaulkner.com/programs/GTFO/Gamertag Follower.zip");
                //Set the filter to only the file type of the newest program. (.exe/.rar/.zip/.7z)
                saveFileDialog1.Filter = "." + Extension + " Files|*." + Extension;
                //Set the title of the save file dialog
                saveFileDialog1.Title = "Choose Save Destination";
                //Set the file name of the silve file dialog
                saveFileDialog1.FileName = "GamerTag Follower";
                //If the person hits "Cancel" instead of "Save" then,
                if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
                    //Do nothing
                    return;
                    //But if they click, "Save,"
                else
                {
                    //Get the URL of the newest version of the program.
                    Uri uri = new Uri("http://www.gagefaulkner.com/programs/GTFO/Gamertag Follower.zip");
                    //The download it with the file name that it was given to the selected place in the save file dialog.
                    webClient1.DownloadFileAsync(uri, saveFileDialog1.FileName);
                    //You're almost done. :)
                }
            }
        }
And the last code is for when the newest version has finished downloading:

Code: Select all

private void webClient1_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //Tell the user that the file has completed downloading and exit the application.
            MessageBox.Show("The newest version of this program has finished downloading!", "Please Install Newest Version", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            Application.Exit();
        }
If you need the code for a progress bar or percent, then I can put it here for you.
Also, your best bet for this code is to start a new thread so that it isn't running off of the UI.