Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2008, 05:25 AM
Registered User
 
Join Date: Apr 2008
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

 
Old November 16th, 2008, 06:32 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old November 17th, 2008, 01:34 AM
Registered User
 
Join Date: Nov 2008
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

 
Old November 18th, 2008, 01:07 PM
Registered User
 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Rod






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





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