Can someone post an example of opening a text file from c#. Thanks.
Can someone post an example of opening a text file from c#. Thanks.
Bryan Thatcher
Structural Design Technician
Clark-Nexsen Architecture & Engineering
Here's one way to read a text file one line at a time:
Code:using System; using System.IO; namespace Sample { class Class1 { void readFile(string filename) { string line; if (File.Exists(filename)) { try { StreamReader reader = new StreamReader(filename); while ((line = reader.ReadLine()) != null) { // do something with input line } } catch (Exception ex) { // failure during read } } } } }
You can use the using statement that automaltially manages clsing of file and meneory space ..
try
{
using( StreamReader reader = new StreamReader(filename))
{
while ((line = reader.ReadLine()) != null)
{
// do something with input line
}
}
}
I know this thread is from a little while ago... Just in case the OP (or someone else) wanted to actually open a text file, as in with Notepad, here's an example which includes a sub-function which accepts a single argument:
Tested successfully using Civil 3D 2011Code:using Autodesk.AutoCAD.Runtime; public class FOO { public static void _OpenFile(string filePath) { System.Diagnostics.Process.Start(filePath); //<-- This is what opens the .TXT file } [CommandMethod("OpenFile")] public void OpenFile() { _OpenFile("<FilePath>\<FileName>.txt"); } }
Last edited by RenderMan; 2011-09-08 at 09:04 PM.
"Potential has a shelf life." - Margaret Atwood