// Extract data from the directory static void ExtractImageData() { Student student = new Student() { AbsoluteUrl = Constants.FTP.BaseUrl }; string directory = "200423859 Junseob Noh"; // To store StudentId, FirstName, and LastName student.FromDirectory(directory); string infoFilePath = student.FullPathUrl + "/" + Constants.Locations.InforFile; bool fileExists = FTPApp.FileExists(infoFilePath); if (fileExists == true) { byte[] bytes = FTPApp.DownloadFileBytes(infoFilePath); string csvData = Encoding.Default.GetString(bytes); string[] csvlines = csvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries); if (csvlines.Length != 2) { Console.WriteLine("Error in CSV format"); } else { student.FromCSV(csvlines[1]); Console.WriteLine(">>> myimage.jpg file as Base64:"); Console.WriteLine(student.ImageData); } } }
static List <Student> CollectStudentInfor() { List <Student> students = new List <Student>(); // fetch the diretoreis info files and Images, and then display the console them. List <string> directories = FTPApp.GetDirectory(Constants.FTP.BaseUrl); foreach (var directory in directories) { Student student = new Student() { AbsoluteUrl = Constants.FTP.BaseUrl }; // To store StudentId, FirstName, and LastName student.FromDirectory(directory); string infoFilePath = student.FullPathUrl + "/" + Constants.Locations.InforFile; bool fileExists = FTPApp.FileExists(infoFilePath); if (fileExists == true) { byte[] bytes = FTPApp.DownloadFileBytes(infoFilePath); string csvData = Encoding.Default.GetString(bytes); string[] csvlines = csvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries); if (csvlines.Length != 2) { Console.WriteLine($"{student.FirstName} {student.LastName} has Error in CSV format"); } else { student.FromCSV(csvlines[1]); } if (student.StudentId == "200423859") { student.MyRecord = true; } else { student.MyRecord = false; } } else { Console.WriteLine($"{student.FirstName} {student.LastName} Could not find info file:"); } students.Add(student); } return(students); }
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 }