streamwriter

Save file - xmlSerializer

谁都会走 提交于 2019-12-05 04:46:21
I'm creating a method to serialize a file using this code: public void Save(Object file, Type type, String path) { // Create a new Serializer XmlSerializer serializer = new XmlSerializer(typeof(type)); // Create a new StreamWriter StreamWriter writer = new StreamWriter(@path); // Serialize the file serializer.Serialize(writer, file); // Close the writer writer.Close(); } But Visual Studio tells me this when I attempt to build: "Error 1 The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?) c:\users\erik\documents\visual studio 2013

c# replace string within file

安稳与你 提交于 2019-12-04 15:37:15
问题 String.Replace doesn't seem to work properly when replacing a portion of an HTML file's content. For example, String.Replace replaces </body></html> with blah blah blah </body></html> html> - notice the second HTML closing tag is not properly closed and therefore shows up when the page is rendered in the browser by the user. Anyone know why it's not working as intended? StreamReader sr = fi.OpenText; String fileContents = sr.ReadToEnd(); sr.close(); fileContents = fileContents.Replace("<body>

Do I need to do StreamWriter.flush()?

╄→гoц情女王★ 提交于 2019-12-04 08:51:55
问题 Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter = new StreamWriter(stream); BinaryWriter binaryWriter = new BinaryWriter(stream); foreach(...) { binaryWriter.Write(number); normalWriter.WriteLine(name); //<~~ easier to reader afterward. } return MemoryStream.ToArray(); } My questions are: Do I need to use flush inside the loop to preserve order? Is returning MemoryStream.ToArray() legal? I using the using -block as a convention, I'm afraid it

File.AppendText attempting to write to wrong location

為{幸葍}努か 提交于 2019-12-04 07:09:56
问题 I have a C# console application that runs as a scheduled task within Windows Task Scheduler. This console app writes to a log file that when runs within debug mode creates and writes to the file within the application folder itself. However, when it runs in the task scheduler it throws an error saying that access is denied because it is trying to write to the same log file, but for some reason it is trying to write to it within the windows\system32 folder. Why would this be happening? And how

How to Write to a file using StreamWriter?

只愿长相守 提交于 2019-12-04 06:00:35
in my Wpf app I'm using a class Person (that is a base class), and that contains a virtual method SaveData(), and other class Client that inherits from Person. How to override method SaveData() and keeping data from base? Class Person public virtual void SaveData() { string arqName = string.Format("Person{0}" + ".txt", Id); StreamWriter file = new StreamWriter(arqNome); file.WriteLine("ID: " + Id); file.WriteLine("DOB: " + dOB); file.WriteLine("Name: " + name); file.WriteLine("Age: " + age); file.Flush(); file.Close(); } Class Client public override void SaveData() { base.SaveData(); string

Creating text file in C#

折月煮酒 提交于 2019-12-04 04:50:38
问题 I'm learning how to create text file in C# but I have a problem. I used this code: private void btnCreate_Click(object sender, EventArgs e) { string path = @"C:\CSharpTestFolder\Test.txt"; if (!File.Exists(path)) { File.Create(path); using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine("The first line!"); } } else if (File.Exists(path)) MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } When I press the "Create" button,

VB - Writing to a file with StreamWriter

被刻印的时光 ゝ 提交于 2019-12-04 04:29:46
问题 I'm trying to write to a file using the StreamWriter. Dim write as IO.StreamWriter write = new io.streamwriter(file) write.write(txtEncryption.text) write.close I stopped the code in debug mode and i saw it crashes and goes straight to the exception when reaches line 2. It is because i just made the file and it's still in use ? How can i avoid that ? 回答1: Dim write As IO.StreamWriter Try write=New IO.StreamWriter(file) write.write(txtEncryption.text) Catch ex As Exception 'Prompt error

Getting the file size from StreamWriter [closed]

余生长醉 提交于 2019-12-04 01:33:22
using (var writer = File.CreateText(fullFilePath)) { file.Write(fileContent); } Given the above code, can the file size be known from StreamWriter ? Yes, you can, try the following long length = writer.BaseStream.Length;//will give unexpected output if autoflush is false and write has been called just before Note: writer.BaseStream.Length property can return unexpected results since StreamWriter doesn't write immediately. It caches so to get expected output you need AutoFlush = true writer.AutoFlush = true; or writer.Flush(); long length = writer.BaseStream.Length;//will give expected output I

C# Writing To Text File Safely With Heaps of Threads

佐手、 提交于 2019-12-03 21:22:58
Okay so my program has around 30 threads, and I need to make a log of some of the information gathered through each threads. The way I did this is by: Creating a public static StringBuilder in the class Program, and then each thread will call the AppendLine method to this StringBuilder instance every few minutes. Then, every 30 minutes another Thread dedicated to writing using a StreamWriter does exactly that. For example: public class Program { public static StringBuilder sb = new StringBuilder(); public static void Main(string[] args) { // Start all threads } } public class Example { Thread

Write mutiple lines to a text file using Visual Basic

会有一股神秘感。 提交于 2019-12-03 20:57:53
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 End If ' Try to create the text file with all the info in it file = System.IO.File.Create("c: