views:

81

answers:

0

Hi there, i'm trying to run this code:

remoteProducts.ForEach(delegate(RemoteProduct p)
                {
                    this.toolStripStatus.Text = "Downloading product: " + this.progress.Value + "/" + remoteProducts.Count;

                    try
                    {
                        prodLocalDao.addProduct(p.Id, p.Category, p.SubCategory, p.Sku);
                        int localProductId = p.Id;

                        List<ProductAttribute> attributes = prodRemoteDao.getProductAttributesByProductId(p.Id);
                        for (int i = 0; i < attributes.Count; i++)
                        {
                            prodLocalDao.addAttribute(localProductId, attributes[i].Id, attributes[i].Name, attributes[i].Value);
                        }

                        // Add photos
                        ArrayList photos = prodRemoteDao.getPhotosFromRemoteByProductId(p.Id);
                        DirectoryInfo directory = new DirectoryInfo(String.Format("photos\\{0}\\", p.Id));
                        if (!directory.Exists) directory.Create();

                        for (int i = 0; i < photos.Count; i++)
                        {
                            FileInfo file = new FileInfo(photos[i].ToString());
                            this.DownloadFTP(photos[i].ToString(), directory.FullName + file.Name);
                        }

                        this.toolStripStatus.Text += "... Done";
                    }
                    catch
                    {
                        this.toolStripStatus.Text += "... Skiped";
                    }


                    this.progress.Value++;
                    this.Refresh();
                });

And it downloads images for the first product correctly, yet when it tries to run this line for the second product:

prodLocalDao.addProduct(p.Id, p.Category, p.SubCategory, p.Sku);

it crashes my application saying vhost has stopped working and these are the details it shows:

Problem signature: Problem Event Name: APPCRASH Application Name: Desktop Manager.vshost.exe
Application Version: 9.0.21022.8
Application Timestamp: 47316898
Fault Module Name: winhttp.dll Fault Module Version: 6.1.7600.16385 Fault Module Timestamp: 4a5bdb3e Exception Code: c0000005 Exception Offset: 000015cb OS Version: 6.1.7600.2.0.0.256.1 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&amp;clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt

DownloadFtp Function

public void DownloadFTP(string RemotePath, string LocalPath)
        {
            BasicFTPClient MyClient = new BasicFTPClient();
            MyClient.Host = Desktop_Manager.Properties.Settings.Default["ftphost"].ToString();
            MyClient.Username = Desktop_Manager.Properties.Settings.Default["ftpuser"].ToString();
            MyClient.Password = Desktop_Manager.Properties.Settings.Default["ftppass"].ToString();


            string remotePath = String.Format("/media/catalog/product{0}", RemotePath);
            MyClient.DownloadFile(remotePath, LocalPath);
        }

And now the BasicFTPClient Class

using System;
using System.Net;
using System.IO;

namespace BasicFTPClientNamespace
{
    class BasicFTPClient
    {
        public string Username;
        public string Password;
        public string Host;
        public int Port;

        public BasicFTPClient()
        {
            Username = "anonymous";
            Password = "[email protected]";
            Port = 21;
            Host = "";
        }

        public BasicFTPClient(string theUser, string thePassword, string theHost)
        {
            Username = theUser;
            Password = thePassword;
            Host = theHost;
            Port = 21;
        }

        private Uri BuildServerUri(string Path)
        {
            return new Uri(String.Format("ftp://{0}:{1}/{2}", Host, Port, Path));
        }

        /// <summary>
        /// This method downloads the given file name from the FTP server
        /// and returns a byte array containing its contents.
        /// Throws a WebException on encountering a network error.
        /// </summary>

        public byte[] DownloadData(string path)
        {
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            return request.DownloadData(BuildServerUri(path));
        }

        /// <summary>
        /// This method downloads the FTP file specified by "ftppath" and saves
        /// it to "destfile".
        /// Throws a WebException on encountering a network error.
        /// </summary>
        public void DownloadFile(string ftppath, string destfile)
        {
            // Download the data
            byte[] Data = DownloadData(ftppath);

            // Save the data to disk
            FileStream fs = new FileStream(destfile, FileMode.Create);
            fs.Write(Data, 0, Data.Length);
            fs.Close();
        }

        /// <summary>
        /// Upload a byte[] to the FTP server
        /// </summary>
        /// <param name="path">Path on the FTP server (upload/myfile.txt)</param>
        /// <param name="Data">A byte[] containing the data to upload</param>
        /// <returns>The server response in a byte[]</returns>

        public byte[] UploadData(string path, byte[] Data)
        {
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            return request.UploadData(BuildServerUri(path), Data);
        }

        /// <summary>
        /// Load a file from disk and upload it to the FTP server
        /// </summary>
        /// <param name="ftppath">Path on the FTP server (/upload/myfile.txt)</param>
        /// <param name="srcfile">File on the local harddisk to upload</param>
        /// <returns>The server response in a byte[]</returns>

        public byte[] UploadFile(string ftppath, string srcfile)
        {
            // Read the data from disk
            FileStream fs = new FileStream(srcfile, FileMode.Open);
            byte[] FileData = new byte[fs.Length];

            int numBytesToRead = (int)fs.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fs.Read(FileData, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0) break;

                numBytesRead += n;
                numBytesToRead -= n;
            }
            numBytesToRead = FileData.Length;
            fs.Close();

            // Upload the data from the buffer
            return UploadData(ftppath, FileData);
        }

    }
}

Any ideas why??!!

thanks...