using System.IO;
Save As Dialog Box
String SaveAsFilename;
//If last directory is not valid then default to My Documents
if (!Directory.Exists(Path.GetDirectoryName(LastFileDirectory)))
LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//----- SAVE FILE DIALOG BOX -----
SaveFileDialog SelectFileDialog = new SaveFileDialog();
SelectFileDialog.Filter = "Comma Separated Values (*." + MY_FILE_EXTENSION + ")|*." + MY_FILE_EXTENSION; //"txt files (*.txt)|*.txt|All files (*.*)|*.*"
SelectFileDialog.FilterIndex = 1; //(First = 1, not 0)
SelectFileDialog.Title = "Save Live Log As";
SelectFileDialog.OverwritePrompt = true;
try
{
SelectFileDialog.InitialDirectory = LastFileDirectory;
}
catch (Exception)
{
SelectFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
//Display dialog box
if (SelectFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) //This ensures correct filename extension is used
{
//----- SAVE THE FILE -----
SaveAsFilename = SelectFileDialog.FileName;
//STORE LAST USED DIRECTORY
if (SaveAsFilename.LastIndexOf("\\") >= 0)
LastFileDirectory = SaveAsFilename.Substring(0, (SaveAsFilename.LastIndexOf("\\") + 1));
else
LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Write StringTo A New File
StreamWriter StreamWriter1= new StreamWriter("C:\\test.txt");
//StreamWriter StreamWriter1= new StreamWriter("C:\\test.txt", false, System.Text.Encoding.Unicode); //False = overwrite - Alternative method specifying the encoding
StreamWriter1.Write(MyString);
StreamWriter1.Close();
Write Bytes To A New File
(Write unsigned char or arrays of unsigned char to a file – not strings or anything else)
FileStream HeaderOutputFile = new FileStream(MyFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
HeaderOutputFile.WriteByte(0x01);
//...
HeaderOutputFile.Close();
Write Binary (Inc Strings) To A New File
The problem with writing using FileStream is that its not string friendly. You can write strings using the StreamWriter, but if you want to write strings and binary data then it's easier to use the Binary Writer.
String sTemp;
FileStream MyOutputFile = new FileStream("C:\\test.txt", FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriter MyOutputWriter = new BinaryWriter(MyOutputFile);
sTemp = "Hello World!\r\n";
MyOutputWriter.Write(sTemp.ToCharArray()); //ToCharArray to avoid String type byte being written at start of the line
MyOutputWriter.Write(Convert.ToByte(0x01)); //Write will write any variable it is given using as many bytes as needed
//...
MyOutputWriter.Close();
MyOutputFile.Close();
//GOOD IDEA TO ADD THIS TO TRY CATCH BLOCK AND REMOVE THE TWO .CLOSE() LINES ABOVE:-
finally
{
if (MyOutputWriter != nullptr)
MyOutputWriter.Close();
if (MyOutputFile != nullptr)
MyOutputFile.Close();
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.