Re: C# Writing to a file

  • From: "RicksPlace" <ofbgmail@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 8 May 2010 07:14:33 -0400

Hi:
First, I don't think I saw your file type, the ".txt" file type extension in the file path for your file if I remember and that would throw an exception. Below is the MSDN Doc stuff I pulled from the File Class Definition that I thought might help. The complete document can be found at:
http://msdn.microsoft.com/en-us/library/ms143356.aspx
if you want to read up on the File Class.
Here is the copied stuff for the CSharp version:
Copy
using System;
using System.IO;
using
System.Text;
class
Test
{
public static void
Main()
{
string path = @"c:\temp\MyTest.txt"
;
// RtNote, notice the .txt file extension in the above file path
// Also, note the use of the @ sign as Jamal identified in a prior post.
// This text is added only once to the file.
if
(!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome"
+ Environment.NewLine;
File.WriteAllText(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text"
+ Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string
readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}

----- Original Message ----- From: "Jamal Mazrui" <empower@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Cc: "Celia Rodriguez" <celia-rodriguez@xxxxxxxxxxxxx>
Sent: Saturday, May 08, 2010 6:34 AM
Subject: Re: C# Writing to a file


In C#, you do not have to double the backslash characters if you precede the string literal with the @ symbol. Just put that symbol immediately before the initial quote mark.

Jamal


On 5/7/2010 11:32 PM, Celia Rodriguez wrote:
Thank you, but I tried the \\ and it is still giving me the following error

Error 1 Unexpected character '\' Error 1 Unexpected
character '\' -----
Thankyou,



Original Message-----




From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Tyler
Littlefield
Sent: Friday, May 07, 2010 9:18 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: C# Writing to a file

Hello,
you need to escape your \ by doing \\. So, it'll look like:
C:\\myfolder1\\myfolder2...

----- Original Message -----
From: "Celia Rodriguez"<celia-rodriguez@xxxxxxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Sent: Friday, May 07, 2010 8:15 PM
Subject: C# Writing to a file


Hi Varun,

I tried writing to a file, but the comiler does not like the ?
\.  The following is what I coded.
File.AppendAllText(C:\Documents and Settings\celia\My Documents\Visual
Studio\Projects\TestCode,Data);

It gives me an error on the \.  Do you know what I need to do so it will
except the \?

Thanks,
Celia

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: