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) 2012 by Dr. Herong Yang,

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("Name: {0}", cmdFile.Name);
      Console.WriteLine("Directory: {0}", cmdFile.Directory);
      Console.WriteLine("FullName: {0}", cmdFile.FullName);
      Console.WriteLine("Last Write Time: {0}", 
      Console.WriteLine("Length: {0}", cmdFile.Length);

Compile it with .NET SDK 4.6.1 and run it:



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!

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

System.IO.FileInfo Class

 What Is FileInfo?

 Public Properties and Methods of FileInfo Class

Creating FileInfo Objects

 FileVersionCopyFile.cs - Testing CopyTo() Method

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials


 PDF Printing Version