Lesson 7 | Writing a text file |
Objective | Use the FileSystemObject to create and write a file. |
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Hosting; using System.IO; public class WriteFileModel : PageModel { private readonly IWebHostEnvironment _env; public string? FilePath { get; private set; } public WriteFileModel(IWebHostEnvironment env) { _env = env; } public void OnGet() { // Define the relative file path var fileName = Path.Combine(_env.ContentRootPath, "App_Data", "testfile.txt"); // Ensure the directory exists Directory.CreateDirectory(Path.GetDirectoryName(fileName)!); // Write content to the file System.IO.File.WriteAllText(fileName, "Just writin' a little text."); // Save path to show in UI (optional) FilePath = fileName; } }
@page @model WriteFileModel @{ ViewData["Title"] = "Write File"; }
Text has been written to the file.
@if (Model.FilePath != null) {Location: @Model.FilePath
}
/Pages └── WriteFile.cshtml └── WriteFile.cshtml.cs /App_Data/ <-- Writable directory (auto-created)
https://<your-app-name>.azurewebsites.net/WriteFile
App_Data/testfile.txt
Read(n) | Reads n characters from the text file; returns a character string |
ReadLine | Reads an entire line; does not include the new line character. Returns a character string. |
ReadAll | Reads the entire file; returns a character string. |
Close | Closes the file when you are finished. |
FileExists | Does the file exist (True/False)? This can be used as part of an If/Then statement. |
Write(string) | Writes a string to the open text file. |
WriteLine(string) | Writes a string to the text file plus a new line character. |
WriteBlankLines(n) | Writes n new line characters to the file. |
Set fileSys = Server.CreateObject("Scripting.FileSystemObject") textFileName = "c:\testfile.txt" Set textStream = fileSys.CreateTextFile(textFileName, True) textStream.WriteLine("Just writin' a little text.") textStream.Close
Set fileSys = Server.CreateObject("Scripting.FileSystemObject") textFileName = "C:\testfile.txt" Set textStream = fileSys.CreateTextFile(textFileName, True) textStream.WriteLine("Just writin' a little text.") textStream.Close
Server.CreateObject("Scripting.FileSystemObject")
: Instantiates the FileSystemObject, which is used for file I/O.CreateTextFile(textFileName, True)
: Creates a new text file (or overwrites it if True
).WriteLine(...)
: Writes a line of text to the file.Close
: Closes the file stream.textFileName = "c:\testfile.txt" Set textStream = fileSys.CreateTextFile(textFileName, True)These lines specify the name of the file to be created, establishing a new TextStream to that file. The "True" indicates that any existing file of the same name is to be overwritten. (False is the default if it is left blank.)
textStream.WriteLine("Just writin' a little text.")This is the text to be written to the file
textStream.CloseThis closes the text stream and the file
FileExists
method. If it does not, you use the CreateTextFile
method to create it; if it does, use the OpenTextFile
method to open it and append data