streamwriter

Redirect .NET StreamWriter output to a String variable

I'd like to know if it is possible to redirect StreamWriter output to a variable Something like String^ myString; StreamWriter sw = gcnew StreamWriter([somehow specify myString]) sw->WriteLine("Foo"); then myString will contain Foo. The reason I would like to do this is to reuse a complex function. I should probably refactor it into ...

Started Process from .NET but RedirectedStandardOutput doesn't support UTF-8

I am trying to call php's HTML purifier from .NET using this code: Process myProcess = new Process(); myProcess.StartInfo.FileName = "C:\Path\to\php.exe"; myProcess.StartInfo.Arguments = "C:\Path\to\purify.php"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardOutput = true; myPro...

Having problem opening/writing to a text file in ASP.NET

I want to write some stats to a text file every time a person loads a page. But every once in awhile I am getting at 'Could Not Open File, Already in use' type of error. I can not 100% replicate this error it is very erratic. My code is Public Sub WriteStats(ByVal ad_id As Integer) Dim ad_date As String = Now.Year & Now.Month ...

Why doesn't StreamWriter work in a Windows Service?

Hi community, I have this simple code that records appends a log to a text file: public static void RecordToFile(string filename, Log log) { TextWriter textWriter = new StreamWriter(Constants.APP_PATH + "\\" + filename, true); textWriter.WriteLine(log.ToString()); textWriter....

Why does the .NET Framework StreamReader / Writer default to UTF8 encoding?

I'm just looking at the constructors for StreamReader / Writer and I note it uses UTF8 as default. Anyone know why this is? I would have presumed it would have been a safer bet to default to Unicode. ...

Streamwriter writes but gives web exception

Hi - I am trying to insert data via web service. The code below writes to the database; however, I have an error (see bottom). What goes wrong here? and how to fix it? //Create the web request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); //Set type to POST request.Method = "POST"; request.ContentType = "text/XML"; ...

Do I need to do StreamWriter.flush() ?

suppose this code (C#): 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. } retu...

StreamWriter - Not appending to created file

I am using the StreamWriter object to write to either a file that is created by the constructor or already exists. If the file exists then it appends data. If not, then it should create a file and then also append data. The problem is that when the file needs to be created, the StreamWriter constructor creates the file but does not write...

Writing file to web server - ASP.NET

Hello all... I simply want to write the contents of a TextBox control to a file in the root of the web server directory... how do I specify it? Bear in mind, I'm testing this locally... it keeps writing the file to my program files\visual studio\Common\IDE directory rather than my project directory (which is where I assume root is when...

StreamWriter .NET - how to get the best performance? C# .Net

What is the recommended approach to get the best performance when we need to create text files bigger than 10MB. There are multiple sections in the code that need to write stuff to a single file. This means a lot of text lines. Option #1: (This logic will be called at several times) Create a StreamWriter instance Write some lines (a...

Delete file using File.Delete and then using a Streamwriter to create the same file?

public void LoadRealmlist() { try { File.Delete(Properties.Settings.Default.WoWFolderLocation + "Data/realmlist.wtf"); StreamWriter TheWriter = new StreamWriter(Properties.Settings.Default.WoWFolderLocation + "Data/realmlist.wtf"); TheWriter.WriteLine("this is my test string"); ...

StreamWriter not writing to an existing file

I am trying to write some text to the file using StreamWriter and getting the path for the file from FolderDialog selected folder. My code works fine if the file does not already exist. but if the file already exist it throws the Exception that the file is in used by other process. using(StreamWriter sw = new StreamWriter(FolderDialog.S...

StreamWriter won't flush to NetworkStream

Using a StreamWriter to write to a NetworkStream, and a StreamReader to read the response. The app is sending commands and reading responses to a news server. Simplified code (sans error handling, etc.): tcpClient = new TcpClient(); tcpClient.Connect(Name, Port); networkStream = tcpClient.GetStream(); serverReader = new StreamReader(...

Power Loss after StreamWriter.Close() produces blank file, why!?

Ok, so to explain; I am developing for a system that can suffer a power failure at any point in time, one point that I am testing is directly after I have written a file out using a StreamWriter. The code below: // Write the updated file back out to the Shell directory. using (StreamWriter shellConfigWriter = new StreamWriter(@"...

StreamWriter Response output - looks fine in Notepad but accents are garbage in Excel

Hi there, I'm using a technique from another stackoverflow question here, to write a CSV file to the Response output for a User to Open/Save. This is working fine and the file looks good in Notepad, but when I open it in Excel the accented characters are garbage. I assumed this was something to do with the character encoding, so tried m...

Write a file to desktop of current user in C# asp.net

if i have this code, this creates a problems if other user that dont have acces to the network share tryes to run the method. How can i make this method write the file to the current users desktop? private void skriveTilFil() { StreamWriter writer = new StreamWriter(@"\\192.168.1.2\tmp\script.vbs"); ...

c# HttpWebRequest POST'ing failing

So i'm trying to POST something to a webserver. System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("url"); System.String Content = "id=" + Id; EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content); EventReq.Method = "POST"; EventReq.ContentType = "application/x-www-form-ur...

How to handle concurrent file access with a filestream/streamwriter?

I am writing an audit file that is writing the username, time, and the old/changed values of several variables in the application for each user when they use my application. It is using a FileStream and StreamWriter to access the audit file. All audits for each user will be written to the same file. The issue is that when two users ...

Streamwriter: Polish characters are skipped?

I'm trying to make a small tool to help some guys converting data between a SAP installation and a Axapta installation. I get a text file i Western European (Windows) encoding (1252). They have put in some special chars to replace some Polish characters. Now it's my job to replace those special chars with the correct Polish characters. ...

Adding a Line to the Middle of a File with .NET

Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example: Hello my name is Brandon, I hope someone can help, //I want the string under this line. Thank you. Hopefully ...