Delete a File on Certain Attempts by Invalid User

Introduction

In one of my previous articles I explained How to Export SQL Data to Excel Using ASP.NET.

This article explains how to delete a file on certain attempts by an invalid user.

We often create too many confidential files that we don't want anyone to open them, it might be possible that you are ready to loose the file rather than show it to anyone, in those cases your system should automatically delete those files if it finds that the user is not valid.

Today I will create an application that will provide three attempts to the user to prove his identity, if he is unable to prove himself then the file will be deleted.

Let's see the procedure to create such an application.

Step 1

First I created two text files, the first one is the main file that we don't want to show to anyone and the second one for maintaining a count of the number of attempts by the user.

These files are named "anu1.txt" and "check.txt".

Then I created a new ASP.NET application in which a TextBox and a Button are used, in the TextBox the user will need to provide his name and then nust click on the button so that the file can be opened.

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Label runat="server" ID="lbl1">Provide Your Name: </asp:Label>

        <asp:TextBox runat="server" ID="txt1"></asp:TextBox>

        <br />

    <asp:Button runat="server" BackColor="Blue" ForeColor="White"
   
 Width="80px" Height="30px" ID="btn1" Text="open" OnClick="btn1_Click" />

    </div>

    </form>

</body>

Step 2

I have created this application as simple as I can, you can modify it and can increase the complexity depending on your needs.

Now I will work on the code behind of this application that is the main part of this article. Write this code in the Code Behind:

        protected void btn1_Click(object sender, EventArgs e)

        {

            string fileLoc = @"D:\anu1.txt";

            string chkFile = @"D:\check.txt";

            if (File.Exists(fileLoc))

            {

                if (txt1.Text != "Anubhav")

                {

                    using (StreamReader tr = new StreamReader(chkFile))

                    {

                        var nn = Convert.ToInt64(tr.ReadLine());

                        tr.Close();

                        if (nn < 3)

                        {

                            if (nn == 2)

                            {

                                Response.Write("This is Your Last Attempt");

                            }

                            var x = nn + 1;

                            File.WriteAllText(@"D:\check.txt", x.ToString());

                        }

                        if (nn >= 3)

                        {

                            var y = 0;

                            File.Delete(fileLoc);

                            Response.Write("File has been Deleted");

                            File.WriteAllText(@"D:\check.txt", y.ToString());

                        }

                    }

                }

                else

                {

                    Response.Write("Welcome Anubhav");

                }

            }

        }

You first need to add "using System.IO;" for the namespace because we are using StreamReader and StreamWriter in this application.

Here first I have created an object for both the files, the first one is for the confidential file and the second one is for the file that will maintain the record of clicks made by the user.

Then the code will check whether the requested file is available or not, if it is available then it will check whether the user name matches the authenticated name or not, if it is not matched than the code will check the number of attempts already made by the false user.

If the number of attempts are less than three then it will be incremented by one, also if the number of attempts are equal to two then a message will be displayed that now only one final attempt is left, if the user is still unable to prove his identity then the file is deleted and a message will be displayed about it and then nothing will be left for him because the file is already deleted.

Now our application is created and is ready to get executed.

Output

On running the application you will get an output like this one:

Delete File on Certain Attempts by Invalid User

Here in the TextBox I will provide one name and click on the button. I will first provide a valid user name and you will see that I am getting greetings in the output.

Delete File on Certain Attempts by Invalid User

Now I will provide a fake name and try to open the file.

Delete File on Certain Attempts by Invalid User

As I click on the button the code has started; it will work and you can see that it has changed the number of attempts from zero to one

Delete File on Certain Attempts by Invalid User

Now as I click some more times then a message is displayed that this will be my final attempt, after this the file will be deleted.

 

But I am clicking one more time with the fake name and let's see what will happen.


Now the file has been deleted as displayed in the output message.

The complete code of this application is as follows:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

 

namespace WebApplication53

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void btn1_Click(object sender, EventArgs e)

        {

            string fileLoc = @"D:\anu1.txt";

            string chkFile = @"D:\check.txt";

            if (File.Exists(fileLoc))

            {

                if (txt1.Text != "Anubhav")

                {

                    using (StreamReader tr = new StreamReader(chkFile))

                    {

                        var nn = Convert.ToInt64(tr.ReadLine());

                        tr.Close();

                        if (nn < 3)

                        {

                            if (nn == 2)

                            {

                                Response.Write("This is Your Last Attempt");

                            }

                            var x = nn + 1;

                            File.WriteAllText(@"D:\check.txt", x.ToString());

                        }

                        if (nn >= 3)

                        {

                            var y = 0;

                            File.Delete(fileLoc);

                            Response.Write("File has been Deleted");

                            File.WriteAllText(@"D:\check.txt", y.ToString());

                        }

                    }

                }

                else

                {

                    Response.Write("Welcome Anubhav");

                }

            }

        }

    }

}

Up Next
    Ebook Download
    View all
    Learn
    View all