Results 1 to 4 of 4

Thread: open text file

  1. #1
    All AUGI, all the time Bryan Thatcher's Avatar
    Join Date
    2007-09
    Location
    Raleigh NC
    Posts
    753

    Default open text file

    Can someone post an example of opening a text file from c#. Thanks.
    Bryan Thatcher
    Structural Design Technician
    Clark-Nexsen Architecture & Engineering

  2. #2
    AUGI Addict sinc's Avatar
    Join Date
    2004-02
    Location
    Colorado
    Posts
    1,986

    Default Re: open text file

    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
                    }
                }
            }
        }
    }

  3. #3
    Member
    Join Date
    2011-09
    Posts
    9

    Default Re: open text file

    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
    }
    }
    }

  4. #4
    Moderator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    2,499

    Default Re: open text file

    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:

    Code:
    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");
        }
    }
    Tested successfully using Civil 3D 2011
    Last edited by RenderMan; 2011-09-08 at 09:04 PM.
    "Potential has a shelf life." - Margaret Atwood

Similar Threads

  1. open and read text file
    By bandent in forum AutoLISP
    Replies: 5
    Last Post: 2010-04-15, 08:46 PM
  2. AutoCAD Text window pops-up every time I open a drawing file.
    By Robert.Hall in forum AutoCAD General
    Replies: 3
    Last Post: 2006-05-08, 01:12 PM
  3. Open file dialog box does not open
    By johannvonspiralspine in forum AutoCAD General
    Replies: 5
    Last Post: 2006-02-07, 03:28 PM
  4. Lost File/Open File/Save Dialog Box on Autocad 2000i
    By thegoffs.89856 in forum AutoCAD General
    Replies: 3
    Last Post: 2005-06-30, 09:05 PM
  5. open file dialog box slow to open
    By klanier in forum AutoCAD General
    Replies: 2
    Last Post: 2004-09-09, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •