Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
DotNETFrameworkNotesForProfessionals.pdf
Скачиваний:
32
Добавлен:
20.05.2023
Размер:
1.82 Mб
Скачать

Chapter 16: File Input/Output

Parameter

Details

string path Path of the file to check. (relative or fully qualified)

Section 16.1: C# File.Exists()

using System; using System.IO;

public class Program

{

public static void Main()

{

string filePath = "somePath";

if(File.Exists(filePath))

{

Console.WriteLine("Exists");

}

else

{

Console.WriteLine("Does not exist");

}

}

}

Can also be used in a ternary operator.

Console.WriteLine(File.Exists(pathToFile) ? "Exists" : "Does not exist");

Section 16.2: VB WriteAllText

Imports System.IO

Dim filename As String = "c:\path\to\file.txt"

File.WriteAllText(filename, "Text to write" & vbCrLf)

Section 16.3: VB StreamWriter

Dim filename As String = "c:\path\to\file.txt" If System.IO.File.Exists(filename) Then

Dim writer As New System.IO.StreamWriter(filename) writer.Write("Text to write" & vbCrLf) 'Add a newline writer.close()

End If

Section 16.4: C# StreamWriter

using System.Text; using System.IO;

string filename = "c:\path\to\file.txt";

//'using' structure allows for proper disposal of stream. using (StreamWriter writer = new StreamWriter(filename"))

GoalKicker.com – .NET Framework Notes for Professionals

67

{

writer.WriteLine("Text to Write\n");

}

Section 16.5: C# WriteAllText()

using System.IO; using System.Text;

string filename = "c:\path\to\file.txt"; File.writeAllText(filename, "Text to write\n");

GoalKicker.com – .NET Framework Notes for Professionals

68