TomJ
TomJ

Reputation: 424

How to save a richtextbox data to a textfile without using the dialog?

I'm writing a program which will document how my day has been (very similar to a diary). I have managed to pretty much get the program to do everything i want at this stage apart from one thing. This is to automatically save the richtextbox data to a .txt file with the date and time of creation. I can get it save the file but it saves it in The folder TomJ as Record My Life files.txt if the folder "Record My Life files" hasn't been created, What i would like is for it to be saved in the folder Record My Life files as (date + time).txt.

To sum up my question: How do i edit my code to automatically save the reviewtxtbox in the Folder Record My Life files, with the name date + time?

I am a novice, i've only spent about 6 hours using C# but i've done some other programming stuff before, so i would be greeatful if you could explain your answers simply. :) Thanks The code i need to edit is as follows:

//creat directory for folders
if (!Directory.Exists(@"C:\Users\TomJ\Record My Life files")) 
{
}
else
{
    Directory.CreateDirectory(@"C:\Users\TomJ\Record My Life files");
    MessageBox.Show("Directory Created for Diary Entries");
}

private void savebutton_Click_1(object sender, EventArgs e)
{
    try
    {
        string date = datepckr.Text;
        string time = timetxtbox.Text;
        savefile.FileName = date + time;
        savefile.DefaultExt = "*.txt*";
        savefile.Filter = "TEXT Files|*.txt";
        reviewtxtbox.SaveFile(@"C:\Users\TomJ\Record My Life files\", RichTextBoxStreamType.PlainText);
        MessageBox.Show("Your day has been saved!");
    }
    catch(Exception etc)
    {
        MessageBox.Show("An error Ocurred: " + etc.Message);
    }
}

Upvotes: 0

Views: 2108

Answers (2)

philreed
philreed

Reputation: 2617

Take a look at the File class within System.IO namespace:

http://msdn.microsoft.com/en-us/library/system.io.file.aspx

In particular, you might find File.AppendAllText or File.CreateText useful.

Both take a full file path and file contents as arguments and assuming your program has write permissions on your folder the file will either be appended to, created or replace based on the function call you use.

An example would be:

string folder = @"C:\Users\TomJ\Record My Life files\" 
string fileName = "testFile.txt";  

File.AppendAllText(folder + fileName, "test text to write to the file");

Upvotes: 1

Are
Are

Reputation: 2241

You can use File.WriteAllText(path, contents). Something like that should work:

//creat directory for folders
if (!Directory.Exists(@"C:\Users\TomJ\Record My Life files")) 
{
}
else
{
   Directory.CreateDirectory(@"C:\Users\TomJ\Record My Life files");
   MessageBox.Show("Directory Created for Diary Entries");
}

private void savebutton_Click_1(object sender, EventArgs e)
{
  try
  {
    string content = new TextRange(reviewtxtbox.Document.ContentStart, reviewtxtbox.Document.ContentEnd).Text;
    string date = datepckr.Text;
    string time = timetxtbox.Text;
    string path = @"C:\Users\TomJ\Record My Life files\" + date + time + ".txt";
    File.WriteAllLines(path,content);
    MessageBox.Show("Your day has been saved!");
  }
  catch(Exception etc)
  {
    MessageBox.Show("An error Ocurred: " + etc.Message);
  }
}

Upvotes: 0

Related Questions