p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > C# and C > C# 1.0 > C#
I forgot my password Register Now
Register | FAQ | Members List | Calendar | 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 16th, 2008, 05:25 AM
Registered User
Points: 7, Level: 1
Points: 7, Level: 1 Points: 7, Level: 1 Points: 7, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old November 16th, 2008, 06:32 AM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old November 17th, 2008, 01:34 AM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old November 18th, 2008, 01:07 PM
Registered User
Points: 7, Level: 1
Points: 7, Level: 1 Points: 7, Level: 1 Points: 7, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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



All times are GMT -4. The time now is 02:59 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc