Verify if an Excel File is Password Protected/Encrypted in C#, VB.Net

How to verify is an excel file (.xlsx) is password protected or encrypted in c# , vb.net in windows application .net core 10.
In today's article I will show you a simple tutorial with example how you can verify if an excel file is password protected or encrypted in C# and VB.Net. So for this article first we will create a password protected excel file.

Password Protected Excel
So here I have created a password protected excel file. When we open this file excel will ask for password. So, to verify an excel file is having password or not we will create a new .net core application and install the NuGet package named as "ExcelDataReader"

ExcelDataReader-NuGet-Package

Now we use the below code to verify the excel file encryption status. 

C#.Net

using ExcelDataReader;
using ExcelDataReader.Exceptions;
using System.Text;

namespace WinFormsApp3
{
    public partial class VerifyExcelFileEncrypted : Form
    {
        public VerifyExcelFileEncrypted()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           //Change file path as per you file location
            string excelFilePath = "C:\\Personal\\Files\\Test.xlsx";
            bool status = ExcelStatus(excelFilePath);
            if (status)
            {
                MessageBox.Show("The Excel file is encrypted.");
            }
            else
            {
                MessageBox.Show("The Excel file is not encrypted.");
            }
        }

        private bool ExcelStatus(string filePath)
        {
            try
            {
                using (var stream = File.OpenRead(filePath))
                {
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    using (ExcelReaderFactory.CreateReader(stream)) { }
                }
            }
            catch (InvalidPasswordException)
            {
                return true;
            }
            return false;
        }
    }
} 

VB.Net

Imports ExcelDataReader
Imports ExcelDataReader.Exceptions
Imports System.Text
Imports System.IO
Imports System.Windows.Forms

Namespace WinFormsApp3
    Public Partial Class VerifyExcelFileEncrypted
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            'Change file path as per you file location
            Dim excelFilePath As String = "C:\Personal\Files\Test.xlsx"
            Dim status As Boolean = ExcelStatus(excelFilePath)
            If status Then
                MessageBox.Show("The Excel file is encrypted.")
            Else
                MessageBox.Show("The Excel file is not encrypted.")
            End If
        End Sub

        Private Function ExcelStatus(filePath As String) As Boolean
            Try
                Using stream = File.OpenRead(filePath)
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
                    Using ExcelReaderFactory.CreateReader(stream)
                    End Using
                End Using
            Catch ex As InvalidPasswordException
                Return True
            End Try
            Return False
        End Function
    End Class
End Namespace 
In above code i have taken a windows forma and in this form i have added a button. On button click event i have called a method named as ExcelStaus to verify that the provided excel file is password protected or not. 

Here i am using .net core 10. So the given piece of code is very important in .net core. "Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);" . If you dont use it will throw an exception. The code line "using (ExcelReaderFactory.CreateReader(stream)) { }" us used to verify that file is encrypted or not. Now let's run the code to check the output.

Excel-encrypted-file-verification
Verify Excel File Encrypted.zip 19KiB

Post a Comment