Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on 

C#.NET - Reading data from a text file

I'm building an application that will download a text file of data into a directory in our site.  My buddy is building the portion of the application that goes out and downloads the text file.  My part is reading the text file and importing the data into our database.

What is/are the class/methods that I need to use to read/write data from/to a text file in C#?
.NET Programming

Avatar of undefined
Last Comment
josgood
Avatar of josgood
josgood
Flag of United States of America image

Here's a simple sample

using System;
using System.IO;  // Contains File class

class Demo {
   static void Main(string[] args) {
      // The file can be anywhere you like.
      String path1 = @"C:\Temp\MyFile.txt";
      // In debug mode, running from IDE default location is YourProgram\bin\debug
      String path2 = "InDebugFolder.txt";
      if (!File.Exists(path2)) {
         Console.WriteLine("{0} doesn't exist.",path2);
      } else {
         // A StreamReader is a convenient, read-only, forward-only way to read a text file
         // Placing StreamReader in a using statement ensures the stream gets closed on exit
         using (StreamReader sr = File.OpenText(path2)) {
            Method1(sr);
        }
      }
   }

   static void Method1(StreamReader sr) {
      // Your processing here
   }
}
Avatar of josephdaviskcrm

ASKER

ok, and what if I did want to write to file also?
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo