Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 16th, 2008, 05:25 AM
Registered User
 
Join Date: Apr 2008
Location: Brentwood, CA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default read any ".txt" file

Hi,

I am new to C# and trying to read in any .txt file that will be dropped in a directory, I will not have control of the file name.

Here is a sample of my calling code:

class Program
    {
        static void Main(string[] args)
        {
            FlatFileTo850 t = new FlatFileTo850();

            string data = File.ReadAllText("C:\\src\\partner\\ConsoleApplica tion1\\subdir\\testFile.txt");
         //would like to take out the file name "testFile.txt"
         //and grab any .txt file

      string result = t.Evaluate(ref data);

            Console.WriteLine(result);

   using (StreamWriter sw = new StreamWriter("TestFileXX.txt", true))
   {
        sw.Write(result.Replace("~", "~\r\n"));
   }

            Console.ReadLine();
        }
    }

Thanks in advance for any help.

Rod

Reply With Quote
  #2 (permalink)  
Old November 16th, 2008, 06:32 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can use DirectoryInfo class and the GetFiles method, one overload accepts a search pattern, *.txt, off the top of my head, which will give you all text files. You can then read the data.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old November 17th, 2008, 01:34 AM
Registered User
 
Join Date: Nov 2008
Location: delhi, delhi, India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

From DirectroyInfo to get path of directory and file path in FileINfo
 and then use StreamReader to read any text file

Reply With Quote
  #4 (permalink)  
Old November 18th, 2008, 01:07 PM
Registered User
 
Join Date: Apr 2008
Location: Brentwood, CA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help, I was able get everything working!

Rod

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read txt file from vb6 poupis VB How-To 5 April 23rd, 2008 10:31 PM
need sample code for read txt file lawsoncobol Access VBA 4 July 16th, 2007 08:30 AM
How to read simple txt file from URL? takica General .NET 1 August 6th, 2005 01:02 PM
Read data from a text file (*.txt) adriant42 Excel VBA 2 June 4th, 2004 06:32 PM



All times are GMT -4. The time now is 09:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.