public static string WriteFile(TextFileHandle fileHandle, bool closeFileHandle, string contents, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExist, TextCodepage destinationCodepage, Action <string> logger) { if (fileHandle.TryPrepareForWrite(fileDoesNotExist, fileExist)) { fileHandle.TextCodepage = destinationCodepage; } StreamWriter writer = fileHandle.CreateStreamWriter(); try { logger(string.Format("Writing <{0}>", contents)); writer.Write(contents); writer.Flush(); } finally { if (closeFileHandle) { fileHandle.Close(); } } return(fileHandle.FilePath); }
public void FileExistsTest() { string csvFilePath = @"c:\StockFile\stocklist.csv"; var textFileHandle = new TextFileHandle(); bool expected = true; bool actual = textFileHandle.FileExists(csvFilePath); Assert.AreEqual(expected, actual); }
public TextFileReader(TextFileHandle fileHandle, FileReadOptions readType, TextCodepage codePage, long skipHeaderLines, long skipFooterLines) { System.Diagnostics.Debug.Assert(fileHandle != null); this.fileHandle = fileHandle; this.readType = readType; this.codePage = codePage; this.skipFooterLines = skipFooterLines; this.skipHeaderLines = skipHeaderLines; }
public void ReadFileTest() { string expected = "Item Code"; var textFileHandle = new TextFileHandle(); var fileContents = textFileHandle.GetFileContents(); string[] row = fileContents[0]; string actual = row[0]; Assert.AreEqual(expected, actual); }
/* Loads the DataGridView with contents from the stoclist.csv * file. * fileContents is a list of string arrayscontaining the data * from the text file. Each string array of the list contaisn the * four fields of the column*/ public void LoadDataGridVew(DataGridView dataGridView) { dataGridView.Rows.Clear(); var textFileHandle = new TextFileHandle(); var fileContents = textFileHandle.GetFileContents(); for (int i = 1; i < fileContents.Count; i++) { string[] column = fileContents[i]; dataGridView.Rows.Add("0", column[0], column[1], Convert.ToInt32(column[2]), column[3]); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); /*Create an instance of the textFileHandle class*/ var textFileHandle = new TextFileHandle(); string csvFilePath = @"c:\StockFile\stocklist.csv"; /*If stocklist file exists in the StockFile directory, * load form1 with DataGridView object. If stocklist file * does not exist in the directory, load form 2 with message * to add file in the correct directory*/ if (textFileHandle.FileExists(csvFilePath)) { Application.Run(new Form1()); } else { Application.Run(new Form2()); } }
/* When user click on 'Save button', the DataGridView contents are * updated into the List < String> array to be updated into the csv * text file. * File contents is a list of string arrays where each list contains * 4 string arrays correspondig to 'Item code', 'Item Despcription', * 'Current Count' and 'On Order'.*/ public void SaveNewCurrentCount(DataGridView dataGridView, int xml_style_type) { var textFileHandle = new TextFileHandle(); var fileContents = new List <String[]>(); string[] dataGridContents = new string[4]; for (int i = 0; i < 4; i++) { dataGridContents[i] = dataGridView.Columns[i + 1].HeaderText; } fileContents.Add(dataGridContents); /*Loop through each row in the DataGrid and add it to fileContents)*/ foreach (DataGridViewRow row in dataGridView.Rows) { dataGridContents = new string[4]; for (int i = 0; i < 4; i++) { dataGridContents[i] = row.Cells[i + 1].Value.ToString(); } fileContents.Add(dataGridContents); } var saveSuccess = textFileHandle.UpdateNewCurrentCount(fileContents, xml_style_type); if (saveSuccess && xml_style_type == 1) { MessageBox.Show("Update Successful", " ", MessageBoxButtons.OK); } else if (!saveSuccess) { MessageBox.Show(@"Cannot Load/Update files. Make sure stocklist.csv, XSLT_Format_1.xslt and XSLT_Format_2.xslt files are in c:\StockFile directory", "Error Message", MessageBoxButtons.OK); } }
static void Main(string[] args) { Process p = Process.GetCurrentProcess(); ShowWindow(p.MainWindowHandle, 3); //SW_MAXIMIZE = 3 while (true) { int option = Print_List_Options(); switch (option) { case 1: Create_Folders_For_Month_Everyday_Has_Folder(); break; case 2: Read_Cell_From_All_EXcel_Sheets_And_Save_In_TXT_File(); break; case 3: Read_TXT_File_And_Check_Every_Line_Exists_In_PDF_Files_And_Save_In_TXT_File(); break; case 4: Rename_Files_From_Text_File(); break; case -1: return; default: break; } } void Create_Folders_For_Month_Everyday_Has_Folder() { Console.Write("Please Enter Parent Folder (if Empty Current Folder Is Parent Folder) : "); string parentFolder = Console.ReadLine(); Console.Write("Please Enter Year : "); int year; if (int.TryParse(Console.ReadLine(), out year)) { int month; Console.Write("Please Enter Month : "); if (int.TryParse(Console.ReadLine(), out month)) { int days = DateTime.DaysInMonth(year, month); for (int day = 1; day <= days; day++) { string folderName = parentFolder; if (!string.IsNullOrEmpty(parentFolder)) { folderName += @"\"; } folderName += new DateTime(year, month, day).ToString("yyyy_MM_dd"); DirectoryInfo directoryInfo = Directory.CreateDirectory(folderName); Console.WriteLine("Folder " + directoryInfo.FullName + " Created."); System.Threading.Thread.Sleep(1000); } } } } void Read_Cell_From_All_EXcel_Sheets_And_Save_In_TXT_File() { Console.Write("Please Enter Excel File Path : "); string xlsFile = Console.ReadLine(); Console.Write("Please Enter Excel Row Number : "); int row = int.Parse(Console.ReadLine()); Console.Write("Please Enter Excel Col Number : "); int col = int.Parse(Console.ReadLine()); List <string> data = ExcelFileHandle.GetCellData(xlsFile, row, col); Console.Write("Please Enter Text File Path : "); string txtFile = Console.ReadLine(); TextFileHandle.SaveListString(data, txtFile); } void Read_TXT_File_And_Check_Every_Line_Exists_In_PDF_Files_And_Save_In_TXT_File() { Console.Write("Please Enter TXT File Path (Input): "); string TXT_File_Input = Console.ReadLine(); Console.Write("Please Enter Folder Path That Has PDF Files : "); string PDF_Folder = Console.ReadLine(); Console.Write("Please Enter TEXT File Path (Output) : "); string TXT_File_Output = Console.ReadLine(); List <string> lines = TextFileHandle.ReadListString(TXT_File_Input); string[] files = Directory.GetFiles(PDF_Folder); List <string> result_TXT_File = new List <string>(); foreach (string file in files) { if (Path.GetExtension(file).ToLower() != ".pdf") { continue; } string ResultLineSave = ""; string PDF_File_Content = PDF_FileHandle.ReadPdfFile(file); foreach (string line in lines) { if (PDF_File_Content.Contains(line)) { ResultLineSave = line + ", " + file; break; } } result_TXT_File.Add(ResultLineSave); TextFileHandle.SaveListString(result_TXT_File, TXT_File_Output); } } void Rename_Files_From_Text_File() { Console.Write("Please Enter TXT File Path (Input): "); string TXT_File_Input = Console.ReadLine(); List <string> lines = TextFileHandle.ReadListString(TXT_File_Input); foreach (string line in lines) { string oldPath = line.Split(',')[1]; string newPath = Path.GetDirectoryName(oldPath) + "\\" + line.Split(',')[0] + Path.GetExtension(oldPath); File.Move(oldPath, newPath); } } }