Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:28:57+00:00 2026-06-12T18:28:57+00:00

I am working on a file locker/unlocker application using C# on VS2010. what i

  • 0

I am working on a file locker/unlocker application using C# on VS2010.
what i want is to lock a file with a password using my application and then unlock it any time.

In fact, I used the following code to lock the file, but the file is being locked only while the application is still running; when I close the application, the file is unlocked.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;

namespace LockFile
{
    public enum LockStatus
    {
        Unlocked,
        Locked
    }

    public class LockFilePresenter
    {
        private ILockFileView view;
        private string file2Lock = string.Empty;
        private FileStream fileLockStream = null;

        public LockFilePresenter(ILockFileView view)
        {
            this.view = view;
        }

        internal void LockFile()
        {
            if (string.IsNullOrEmpty(file2Lock) || !File.Exists(file2Lock))
            {
                view.ShowMessage("Please select a path to lock.");
                return;
            }

            if (fileLockStream != null)
            {
                view.ShowMessage("The path is already locked.");
                return;
            }
            try
            {
                fileLockStream = File.Open(file2Lock, FileMode.Open);
                fileLockStream.Lock(0, fileLockStream.Length);
                view.SetStatus(LockStatus.Locked);
            }
            catch (Exception ex)
            {
                fileLockStream = null;
                view.SetStatus(LockStatus.Unlocked);
                view.ShowMessage(string.Format("An error occurred locking the path.\r\n\r\n{0}", ex.Message));
            }
        }

        internal void UnlockFile()
        {
            if (fileLockStream == null)
            {
                view.ShowMessage("No path is currently locked.");
                return;
            }
            try
            {
                using (fileLockStream)
                    fileLockStream.Unlock(0, fileLockStream.Length);
            }
            catch (Exception ex)
            {
                view.ShowMessage(string.Format("An error occurred unlocking the path.\r\n\r\n{0}", ex.Message));
            }
            finally
            {
                fileLockStream = null;
            }
            view.SetStatus(LockStatus.Unlocked);
        }

        internal void SetFile(string path)
        {
            if (ValidateFile(path))
            {
                if (fileLockStream != null)
                    UnlockFile();
                view.SetStatus(LockStatus.Unlocked);
                file2Lock = path;
                view.SetFile(path);
            }
        }

        internal bool ValidateFile(string path)
        {
            bool exists = File.Exists(path);
            if (!exists)
                view.ShowMessage("File does not exist.");
            return exists;
        }
    }
}

and

using System;
using System.Collections.Generic;
using System.Text;

namespace LockFile
{
    public interface ILockFileView
    {
        void ShowMessage(string p);
        void SetStatus(LockStatus lockStatus);
        void SetFile(string path);
    }
}

As I said previously, the application works fine during the running time, but when I close it, the locked file will be unlocked.

If anybody has any idea about how to do it, I would be grateful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:28:59+00:00Added an answer on June 12, 2026 at 6:28 pm

    A Lock on a FileStream just means that your process has exclusive access to the file while it’s active; it has nothing to do with password protecting a file.

    It sounds like what you want is to encrypt a file with a password. The file class provides Encrypt/Decrypt based on the current user, or, if you want it based on your own custom password there’s a sample of using some of the classes in the System.Security.Cryptography namespace to encrypt a file with a password here (instead of hard coding you would take it as input presumably) http://www.codeproject.com/Articles/26085/File-Encryption-and-Decryption-in-C

    Keep in mind, doing security right is hard.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I working on adding file uploading to my web application. I'm using an iframe
I have a working file rename java tool, now I want to add an
This is my first time working with file i/o in java, and it's not
I'm using perforce for versioning control. Let's say I am working on a file
I am using HTML5 fillText method of canvas in my coding.It is working file
Am working on file / folder event capturing of OS X 10.5 and 10.6
On whatever reason this is not working (says 'file not found'), set in=c:\myprogram\_save cd
I am working on pom file modifications to change the version node value. I
I'm working on a file format that should be written and read in several
I am working with a file (fasta file), here is the format- >chr1 AACCCCCCCCTCCCCCCGCTTCTGGCCACAGCACTTAAACACATCTCTGC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.