C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Creating FileInfo Objects
This section provides a tutorial example on how to create a FileInfo object to represent a specific executable file using the FileInfo class constructor FileInfo(fileName).
If you read the reference of FileInfo class, you will see that a constructor method is provided to allow you to create a FileInfo object for a file based on the specified file name.
FileInfo(fileName) is the constructor method that returns a FileInfo object representing the specified file. "fileName" is a string representation of the full path name of the specified file.
Here is a quick sample C# program that shows you how to use the FileInfo(fileName) constructor method.
// FileInfoTest.cs // Copyright (c) 2016 HerongYang.com. All Rights Reserved. using System; using System.IO; public class FileInfoTest { public static void Main() { // Creating the FileInfo object to represent cmd.exe FileInfo cmdFile = new FileInfo("\\Windows\\System32\\cmd.exe"); // Printing out version information of cmd.exe Console.WriteLine(); Console.WriteLine("Name: {0}", cmdFile.Name); Console.WriteLine("Directory: {0}", cmdFile.Directory); Console.WriteLine("FullName: {0}", cmdFile.FullName); Console.WriteLine("Last Write Time: {0}", cmdFile.LastWriteTime); Console.WriteLine("Length: {0}", cmdFile.Length); } }
Compile it with .NET SDK 4.6.1 and run it:
C:\herong>\windows\Microsoft.NET\Framework\v4.0.30319\csc FileInfoTest.cs C:\herong>FileInfoTest.exe Name: cmd.exe Directory: C:\Windows\System32 FullName: C:\Windows\System32\cmd.exe Last Write Time: 11/20/2010 7:17:00 AM Length: 302592
It works!
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Public Properties and Methods of FileInfo Class
FileVersionCopyFile.cs - Testing CopyTo() Method
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation