streamwriter

saving data in csv file

社会主义新天地 提交于 2019-12-23 06:31:12
问题 I have a problem to save data in .csv file. void WriteLog(DataRow rzad) { StreamWriter sw = new StreamWriter("log.csv", true); int iColCount = 8; for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(rzad[i])) { sw.Write(rzad[i].ToString()); sw.Write("\t"); } } sw.Write("\n"); sw.Flush(); sw.Close(); } The problem is tak in file I have data in A column. I want to smash one row in DataRow format to 8 parts, which are put in 8 different columns. My function working as it doesn't see the

How to write an observable collection to a txt file?

廉价感情. 提交于 2019-12-23 04:37:41
问题 Whats the best way to write an observable collection to a txt file? I currently have the following public ObservableCollection<Account> SavedActionList = new ObservableCollection<Account>(); using (System.IO.StreamWriter file = new System.IO.StreamWriter("SavedAccounts.txt")) { foreach (Account item in SavedActionList) { file.WriteLine(item.ToString()); //doesn't work } file.Close(); } I'm not sure why it won't write to the file. Any ideas? 回答1: You can easily just write: File.WriteAllLines(

Writing to a text file from DataGridView in C# windows form

故事扮演 提交于 2019-12-23 04:26:15
问题 I want to write the data from my DataGridView to a textfile. The following code writes the data but it is not formatted correctly. StreamWriter sW = new StreamWriter(dirLocationString); string lines = ""; for (int row = 0; row< numRows1; row++){ for (int col = 0; col < 4; col++) { lines = lines + " " + dataGridView1.Rows[row].Cells[col].Value.ToString(); } sW.WriteLine(lines); } sW.Close(); I want the output format to be like: AAAA, BBBB, CCCC, DDDD But instead it displays the following: AAAA

How to write a text file from a .NET DataGridView control cell values?

无人久伴 提交于 2019-12-21 20:46:43
问题 I have the following code that should loop through all the rows in my DataGridView , and write all their cell values to a text file. However, it outputs all the rows, but only the first cell of each one, and not the other three cells. string file_name = "C:\\test1.txt"; var objWriter = new System.IO.StreamWriter(file_name); int count = dgv.Rows.Count; for (int row = 0; row < count; row++) { objWriter.WriteLine(dgv.Rows[row].Cells[0].Value.ToString()); } objWriter.Close(); 回答1: for (int row =

Write mutiple lines to a text file using Visual Basic

自作多情 提交于 2019-12-21 06:02:03
问题 I'm trying to check to see if a file exists, if so it does nothing. If the file does not exist is creates the text file. Then I want to write text to that file. Where am I going wrong with this code? I'm just trying to write multiple lines to the text file and that part is not working. It is creating the text file... just not writing to it. Dim file As System.IO.FileStream Try ' Indicate whether the text file exists If My.Computer.FileSystem.FileExists("c:\directory\textfile.txt") Then Return

Can any one tell why the previous data is still displayed while saving data using StreamWriter

那年仲夏 提交于 2019-12-20 07:31:51
问题 I am saving data to a file-name using Stream writer but if i run the code for second time the same data is appended to the previous data but i would like to clear the old data and write the data The data i should have in text file should be as follows 101 435435345 3445454541104021031A094101 52251 1 1 CCD1 110402110402 1111000020000001 6281110000251 00000000011 1 1 0111000020000001 822500000100111000020000000000010000000000001 111000020000001

C# Program crashes when Writing using StreamWriter

纵然是瞬间 提交于 2019-12-20 07:15:37
问题 An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll It works only when the file is already created. When i delete the file and start from scratch it gives the following error Code: private void Btn_Create_Click(object sender, EventArgs e) { string path = Environment.CurrentDirectory + "/"+ "File.txt"; if (!File.Exists(path)) { File.CreateText(path); MessageBox.Show("File Created Successfully"); } else { MessageBox.Show("File Already Created"); } } private void Btn

Streamwriter overwriting the text in txtfile

杀马特。学长 韩版系。学妹 提交于 2019-12-20 07:06:19
问题 Is there some way to reopen streamwriter without it creating a new object to write to? Because at the moment, when WriteOdd is called, streamwriter is overwriting WriteEven, which is called before it :/ public void WriteEven() { StreamWriter writer = new StreamWriter(FILENAME); for (int i = 0; i < array.Length; i+= 2) { Console.WriteLine(array[i]); writer.WriteLine("EvenNumbers: " + array[i]); } writer.Close(); } public void WriteOdd() { StreamWriter writer = new StreamWriter(FILENAME); for

Streamwriter overwriting the text in txtfile

爷,独闯天下 提交于 2019-12-20 07:05:59
问题 Is there some way to reopen streamwriter without it creating a new object to write to? Because at the moment, when WriteOdd is called, streamwriter is overwriting WriteEven, which is called before it :/ public void WriteEven() { StreamWriter writer = new StreamWriter(FILENAME); for (int i = 0; i < array.Length; i+= 2) { Console.WriteLine(array[i]); writer.WriteLine("EvenNumbers: " + array[i]); } writer.Close(); } public void WriteOdd() { StreamWriter writer = new StreamWriter(FILENAME); for

C# Client/Server: using Streamreader/writer

旧巷老猫 提交于 2019-12-19 10:34:14
问题 I'm relatively new to C# but here goes: I am developing a remote file service client/server console application in C# which is supposed to exchange messages using synchronous sockets. One of the main problems (even thought it might seem simple) is to return a string from the server, to the client using streamreader/streamwriter. The application user a command line interface with options (from a switch statement) to execute actions. I.e. typing 1 and enter would execute the code to send the