Downtown Programmer's Corner - Demos - C#
Some snippets around file handling in C#.
Get the filename of the currently running program:
string sThisfile = System.Reflection.Assembly.GetEntryAssembly().Location; DirectoryInfo dinfo = new DirectoryInfo(sThisfile); string sThispath = dinfo.Parent.FullName;
Read textfile to array: .
string sFile = "C:\testfile.txt";
string[] a = {};
if (System.IO.File.Exists(sFile)
{
a = System.IO.File.ReadAllLines(sFile);
}
Write array to textfile: .
// ..
string[] aLines = {"Line one.", "Line two.", "Line three."};
System.IO.File.WriteAllLines(@"C:\tmp\output.txt", aLines);
Append lines to textfile: .
.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(sInifile, true))
{
file.WriteLine(s);
file.WriteLine("");
}
.
.
.
|
|
MSDN File Class about the File class, e.g. to copy files ... . | Reference |
|
|
MSDN How to: Write to a Text File (C# Programming Guide). Simple recipe. | Recipe |
|
|
MSDN How to: Read From a Text File (C# Programming Guide). Simple recipe. | Recipe |
|
|
MSDN File.Exists Method simple recipe . | Recipe |
.
Imprint : www.trilo.de/imprint.html