Textfiles

Reading / writing

Reading from textfiles

using System.IO;

using (StreamReader readFile = new StreamReader(SaveLocation))
  vosaRecordsFromFile = readFile.ReadToEnd();

Writing to textfiles

using (StreamWriter writeFile = new StreamWriter("c:\\test.xml"))
{
writeFile.Write("test");
}

Adding to textfiles

StreamWriter sw;
sw = File.AppendText("c:\\test.txt");
sw.WriteLine("text");
sw.Close();

Reading from a Stream

Stream stream = entry.Open();
using (StreamReader reader = new StreamReader(stream))
{
  string contents = reader.ReadToEnd();
}

File system

Check if file exists, delete file …

using System.IO;
Exist: File.Exists
Delete: File.Delete
Exist with wildcard:
string[] files = Directory.GetFiles("C:\\temp", "*_peach.xml", SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
//file exist
}

Check if directory exists

using System.IO;
Directory.Exists()

Paths

string pathFile = Path.Combine(path, filename);

Encoding

Encoding basics

https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-encoding
Some interesting remarks:
https://stackoverflow.com/questions/8121005/what-is-the-default-encoding-when-saving-a-text-file

Encoding defaults

VBA/VB6: Kind of Unicode: UTF-16
https://www.di-mgt.com.au/howto-convert-vba-unicode-to-utf8.html

StreamWriter: UTF-8 (without BOM - byte order mark)
https://stackoverflow.com/questions/21602915/encoding-utf8-as-default

StreamReader: auto-detection, based on BOM - byte order mark, not always working well
https://weblog.west-wind.com/posts/2007/Nov/28/Detecting-Text-Encoding-for-StreamReader

Encoding conversion

Encoding.Convert
https://msdn.microsoft.com/en-us/library/kdcak6ye%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Convert VBA/VB6 Unicode/UTF-16 to UTF-8:
https://www.di-mgt.com.au/howto-convert-vba-unicode-to-utf8.html
Saving VBA to UTF-8:
https://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba

Encoding

Extended ascii characters from the latin1 set (iso-8859-1):
using (StreamReader r = new StreamReader(fileName, Encoding.GetEncoding("iso-8859-1")))

http://stackoverflow.com/questions/8089357/how-to-read-special-character-like-%C3%A9-%C3%A2-and-others-in-c-sharp
http://stackoverflow.com/questions/592824/c-sharp-help-reading-foreign-characters-using-streamreader

Save file dialog

https://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog(v=vs.110).aspx

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
  using (StreamWriter writeFile = new StreamWriter(saveFileDialog1.OpenFile()))
  {
    writeFile.Write("test");
  }
}

Export to Excel

    public static void GridToExcel(DataGridView grid)
    {
      Excel.Application app = new Excel.Application();
      app.Visible = false;
      Excel.Workbook book = app.Workbooks.Add(Excel.XlSheetType.xlWorksheet);
      Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;
      // Write the columnheaders to Excel
      foreach (DataGridViewColumn dgvCol in grid.Columns)
        sheet.Cells[1, dgvCol.Index + 1] = dgvCol.HeaderText;
      // Write the data to Excel
      foreach (DataGridViewRow dgvRow in grid.Rows)
      {
        foreach (DataGridViewCell dgvCell in dgvRow.Cells)
        {
          sheet.Cells[dgvCell.RowIndex + 2, dgvCell.ColumnIndex + 1] = dgvCell.Value.ToString();
        }
      }
      app.Visible = true;
    }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License