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#?
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
}
}
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.
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
}
}