コード例 #1
0
        static void Main(string[] args)
        {
            // 1. Retrieve a list of directories from the FTP
            Console.WriteLine(">>> Retrieve a list of directories");
            GetFtpDirectories();
            Console.WriteLine("====End of 1==================================");

            // 2. Extract data from the image
            Console.WriteLine(">>> Output the image file as Base64");
            ExtractImageData();
            Console.WriteLine("====End of 2==================================");

            // Gathering students information
            List <Student> students = CollectStudentInfor();

            // 3. Output data from ToString() and ToCSV().
            // For my record
            Student myRecord = new Student();

            foreach (var student in students)
            {
                Console.WriteLine("=============================================");
                Console.WriteLine("Output data from ToString()");
                Console.WriteLine(student.ToString());
                Console.WriteLine("---------------------------------------------");
                Console.WriteLine("Output data from ToCSV()");
                Console.WriteLine(student.ToCSV());
                if (student.MyRecord)
                {
                    myRecord = student;
                }
            }

            Console.WriteLine("=============================================");
            Console.WriteLine("Output My Record");
            Console.WriteLine(myRecord.ToString());
            Console.WriteLine("=============================================");

            Console.WriteLine($"The number of students is {students.Count}.");
            int countOfCorrectAges = 0;
            int sumOfAges          = 0;

            int        countOfWrongAge = 0;
            List <int> ages            = new List <int>();
            int        highestAge      = 0;
            int        lowestAge       = 100;

            foreach (var student in students)
            {
                ages.Add(student.Age);
                if (student.Age > 0 && student.Age < 100)
                {
                    countOfCorrectAges++;
                    sumOfAges += student.Age;
                    if (student.Age > highestAge)
                    {
                        highestAge = student.Age;
                    }
                    if (student.Age < lowestAge)
                    {
                        lowestAge = student.Age;
                    }
                }
                else
                {
                    countOfWrongAge++;
                }
            }

            // These results are before modified values.
            Console.WriteLine("===============================================");
            Console.WriteLine("These results are before modified values.");
            Console.WriteLine($"Average of ages of students : {ages.Average()}");
            Console.WriteLine($"Highest age : {ages.Max()}");
            Console.WriteLine($"Lowest age : {ages.Min()}");

            Console.WriteLine("-----------------------------------------------");

            // These results are valid values of the ages of students.
            double avgOfAges = sumOfAges / countOfCorrectAges;

            Console.WriteLine("These results are valid values of the ages of students.");
            Console.WriteLine($"Average of ages of {countOfCorrectAges} students : {avgOfAges}");
            Console.WriteLine($"Highest age : {highestAge}");
            Console.WriteLine($"Lowest age : {lowestAge}");
            Console.WriteLine("-----------------------------------------------");


            //Save to CSV, JSON and XML files, and then uploading the files to My FTP
            SaveFiles(students, "students");
        }
コード例 #2
0
        static void Main(string[] args)
        {
            List <Student> students    = new List <Student>();
            List <string>  directories = Helper.FTP.GetDirectory(Constants.FTP.BaseUrl);

            foreach (var directory in directories)
            {
                Student student = new Student()
                {
                    AbsoluteUrl = Constants.FTP.BaseUrl
                };
                student.FromDirectory(directory);

                Console.WriteLine(student);
                string infoFilePath = student.FullPathUrl + "/" + Constants.Locations.InfoFile;

                bool fileExists = Helper.FTP.FileExists(infoFilePath);
                if (fileExists == true)
                {
                    string csvPath = $@"C:\Users\chirag\Desktop\Students\{directory}.csv";

                    var      downloadedBytes = Helper.FTP.DownloadFileBytes(infoFilePath);
                    string   csvData         = Encoding.Default.GetString(downloadedBytes);
                    string[] csvLines        = csvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
                    if (csvLines.Length != 2)
                    {
                        Console.WriteLine("error in parsing");
                    }
                    else
                    {
                        student.FromCSV(csvLines[1]);
                    }
                    Console.WriteLine("Found info file:");
                }
                else
                {
                    Console.WriteLine("Could not find info file:");
                }

                Console.WriteLine("\t" + infoFilePath);

                string imageFilePath = student.FullPathUrl + "/" + Constants.Locations.ImageFile;

                bool imageFileExists = Helper.FTP.FileExists(imageFilePath);

                if (imageFileExists == true)
                {
                    var   imageBytes = Helper.FTP.DownloadFileBytes(student.MyImagePath);
                    Image myimage    = Helper.ImageConversion.ByteArrayToImage(imageBytes);

                    string base64 = ImageConversion.ImageToBase64(myimage, ImageFormat.Jpeg);
                }
                else
                {
                    Console.WriteLine("Could not find image file:");
                }
                students.Add(student);
                Console.WriteLine("\t" + imageFilePath);
            }


            string studentCSVPath = $"{Constants.Locations.DataFolder}\\students.csv";

            using (StreamWriter fs = new StreamWriter(studentCSVPath))
            {
                foreach (var student in students)
                {
                    fs.WriteLine(student.ToCSV());
                }
            }

            //string studentXMLPath = $"{Constants.Locations.DataFolder}\\students.xml";
            XmlSerializer xmlSerializer = new XmlSerializer(students.GetType());


            //Serialize XML
            //Create an instacne of StringWriter to write the XML string as a Stream
            using (StringWriter textWriter = new StringWriter())
            {
                //Convert the student object to XML
                xmlSerializer.Serialize(textWriter, students);

                //Write out the XML to the Console
                string studentXMLPath = textWriter.ToString();
                Helper.FTP.UploadFile(studentXMLPath, Constants.FTP.BaseUrl + Constants.FTP.remoteUploadFileDestination + "xml", Constants.FTP.Username, Constants.FTP.Password);
            }

            string studentJSONPath = Newtonsoft.Json.JsonConvert.SerializeObject(students);


            Helper.FTP.UploadFile(studentCSVPath, Constants.FTP.BaseUrl + Constants.FTP.remoteUploadFileDestination + "csv", Constants.FTP.Username, Constants.FTP.Password);
            Helper.FTP.UploadFile(studentJSONPath, Constants.FTP.BaseUrl + Constants.FTP.remoteUploadFileDestination + "json", Constants.FTP.Username, Constants.FTP.Password);

            #region Aggregate
            var count = students.Count();
            Console.WriteLine("count is " + count);
            var d = 0; var c = 0;
            foreach (var student in students)
            {
                if (student.ToString().StartsWith("2004"))
                {
                    c++;
                }

                if (student.ToString().Contains("a"))
                {
                    d++;
                }

                if (student.FirstName == "Sahiba" & student.StudentId == "200449112")
                {
                    student.MyRecord = true;
                }
            }
            Console.WriteLine("Count of starts With" + c);
            Console.WriteLine("Count of Contains is " + d);

            var averageAge = students.Average(x => x.age);
            var maxAge     = students.Max(x => x.age);
            var minAge     = students.Min(x => x.age);
            Console.WriteLine("the Average Age is " + averageAge);
            Console.WriteLine("{0} is the MAX age and {1} is the MIN age", maxAge, minAge);
            #endregion
        }