TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Vivek Datta
NA
8
0
No overwriting by streamwriter
Oct 19 2009 10:04 AM
//Hello there, these commented lines are not executing. help me out here. when i run 1st time, a file is created. when i run it second time, it dsnt get //overwritten. plz help me out in this....
namespace File_Experiment
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw;
StreamReader sr;
FileStream fs;
char c;
string pth;
pth = Directory.GetCurrentDirectory();
Console.WriteLine(pth);
pth = pth + "\\" + "textfile.txt";
if ((File.Exists(pth)))
{
Console.WriteLine("The file already exists. Append? y or n");
c = Convert.ToChar( Console.Read());
switch (c)
{
case 'y':
FileInfo fs2;
StreamWriter sw2;
fs2 = new FileInfo(pth);
sw2 = new StreamWriter(pth);
Console.WriteLine("Enter the text to be written in the file");
string line = "hello chaman";
line = Console.ReadLine(); //Why isnt this statement executing??
sw2.WriteLine(line); //Why isnt this statement executing??
Console.WriteLine("The text has been written");
break;
case 'n': break;
default: Console.WriteLine("No identifiable key inserted. The program will now exit");
break;
}
}
else
{
fs = new FileStream(pth, FileMode.Create);
sw = new StreamWriter(fs);
Console.WriteLine("Enter the text to be written in the file");
string line = Console.ReadLine();
sw.WriteLine(line);
Console.WriteLine("The text has been written");
sw.Close(); fs.Close();
}
Console.WriteLine("Thanks for using my program :)");
}
}
}
Reply
Answers (
6
)
Any comments on the reporting tool provided by Microsoft and Crystal reports?
Streamreader