public class FileInfoMainTest
{
public static void Main()
{
// Open an existing file, or create a new one.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
StreamReader sr = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
// Open an existing file, or create a new one.
FileInfo* fi = new FileInfo(S"temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter* sw = fi->AppendText();
sw->WriteLine(S"This is a new entry to add to the file");
sw->WriteLine(S"This is yet another line to add...");
sw->Flush();
sw->Close();
// Get the information out of the file and display it.
StreamReader* sr = new StreamReader(fi->OpenRead());
while (sr->Peek() != -1)
Console::WriteLine(sr->ReadLine());
}
[JScript]
import System;
import System.IO;
public class FileInfoMainTest {
public static function Main() : void {
// Open an existing file, or create a new one.
var fi : FileInfo = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
var sw : StreamWriter = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
var sr : StreamReader = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
FileInfoMainTest.Main();
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fi1 As FileInfo = New FileInfo(path)
If fi1.Exists = False Then
'Create a file to write to.
Dim sw As StreamWriter = fi1.CreateText()
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If
'Open the file to read from.
Dim sr As StreamReader = fi1.OpenText()
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
Try
Dim path2 As String = path + "temp"
Dim fi2 As FileInfo = New FileInfo(path2)
'Ensure that the target does not exist.
fi2.Delete()
'Copy the file.
fi1.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
'Delete the newly created file.
fi2.Delete()
Console.WriteLine("{0} was successfully deleted.", path2)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class