While trying to upload a file in C#, you may need to get file extension or validate file extension from file name in C#, so in this post, I have provided code sample, how you can  check file extension or file size before uploading a file in C#.

Get File Extension from file in C#

Let's take a look at an example, which shows how you can extract file extension from file, in this case we will be using a file which is already on disk. Using FileInfo class we get all the details of the file.

using System;
using System.IO;
using System.Text;


namespace FileSizeAndExtension
{
   
    public class Program
    {
        public static void Main()
        {

            // Write Full File name with directory
            string fileName = @"D:\C#-sample.pdf";
            //FileInfo Class to get file details
            FileInfo fi = new FileInfo(fileName);

            // Get File Name  
            string GetFileName = fi.Name;
            Console.WriteLine("File Name: {0}", GetFileName);
            // Get file name with complete path   
            string fullFileName = fi.FullName;
            Console.WriteLine("File Name: {0}", fullFileName);

            // Get file extension
            string extn = fi.Extension;
            Console.WriteLine("File Extension: {0}", extn);
            // Get directory name   
            string directoryName = fi.DirectoryName;
            Console.WriteLine("Directory Name: {0}", directoryName);

            //Check if File Exists
            bool exists = fi.Exists;
            Console.WriteLine("File Exists: {0}", exists);
            if (fi.Exists)
            {
                // Get file size
                long size = fi.Length;
                Console.WriteLine("File Size in Bytes: {0}", size);

                // Check if File ReadOnly ?  
                bool IsReadOnly = fi.IsReadOnly;
                Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);
            }

            Console.ReadKey();
        }
    }
}

Output:

File Name: C#-sample.pdf
File Name: D:\C#-sample.pdf
File Extension: .pdf
Directory Name: D:\
File Exists: True
File Size in Bytes: 10218145
Is ReadOnly: False

In the above code, we already have a file placed in "D:\C#-sample.pdf", and we are getting file details using FileInfo class in C#, which is in namespace "System.IO".

file-extension-c-sharp-min.png

Get File Extension and File Size using C#

We use Extension property of a file to get it's extension like .txt, .xlsx etc, while Length property of the FileInfo class returns the size of a file in bytes.

Let's take a look at an example to get file size and extension using C#.

using System;
using System.IO;
using System.Text;


namespace FileSizeAndExtension
{
   
    public class Program
    {
        public static void Main()
        {

            // Write Full File name with directory
            string fileName = @"E:\CSharpFile.txt";
            //FileInfo Class to get file details
            FileInfo fi = new FileInfo(fileName);

            // Create this file
            using (FileStream fs = fi.Create())
            {
                Byte[] txt = new UTF8Encoding(true).GetBytes("New file.");
                fs.Write(txt, 0, txt.Length);
                Byte[] author = new UTF8Encoding(true).GetBytes("Author Vikas lalwani");
                fs.Write(author, 0, author.Length);
            }

            // Get File Name  
            string GetFileName = fi.Name;
            Console.WriteLine("File Name: {0}", GetFileName);
            // Get file name with complete path   
            string fullFileName = fi.FullName;
            Console.WriteLine("File Name: {0}", fullFileName);

            // Get file extension
            string extn = fi.Extension;
            Console.WriteLine("File Extension: {0}", extn);
            // Get directory name   
            string directoryName = fi.DirectoryName;
            Console.WriteLine("Directory Name: {0}", directoryName);

            //Check if File Exists
            bool exists = fi.Exists;
            Console.WriteLine("File Exists: {0}", exists);
            if (fi.Exists)
            {
                // Get file size
                long size = fi.Length;
                Console.WriteLine("File Size in Bytes: {0}", size);

                // Check if File ReadOnly ?  
                bool IsReadOnly = fi.IsReadOnly;
                Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);

                // Creation, last access, and last write time   
                DateTime creationTime = fi.CreationTime;
                Console.WriteLine("Creation time: {0}", creationTime);

                DateTime accessTime = fi.LastAccessTime;
                Console.WriteLine("Last access time: {0}", accessTime);

                DateTime updatedTime = fi.LastWriteTime;
                Console.WriteLine("Last write time: {0}", updatedTime);
            }

            Console.ReadKey();
        }
    }
}

Output:

File Name: CSharpFile.txt
File Name: E:\CSharpFile.txt
File Extension: .txt
Directory Name: E:\
File Exists: True
File Size in Bytes: 29
Is ReadOnly: False
Creation time: 28/02/2020 20:20:33
Last access time: 28/02/2020 20:20:33
Last write time: 28/02/2020 20:20:33

In the above code, we are using FileInfo to create the file, get file size and extension, but we are also creating it using FileStream.

get-file-size-c-sharp-min.png

You may also like:

File I/O in C# (Read, Write, Delete, Copy file using C#)

Upload file to google drive using Google Drive API in ASP.NET MVC