永久锁定文件

Has*_*ion 0 c# encryption file-locking filestream

我正在 VS2010 上使用 C# 开发文件锁定器/解锁器应用程序。我想要的是使用我的应用程序使用密码锁定文件,然后随时解锁。

事实上,我使用以下代码来锁定文件,但仅当应用程序仍在运行时才会锁定文件;当我关闭应用程序时,文件被解锁。

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,应用程序在运行时工作正常,但是当我关闭它时,锁定的文件将被解锁。

如果有人知道如何做,我将不胜感激。

lga*_*aud 5

A Lockon aFileStream仅表示您的进程在文件处于活动状态时对其具有独占访问权限;它与保护文件的密码无关。

听起来你想要的是用密码加密文件。文件类提供基于当前用户的加密/解密,或者,如果您希望基于您自己的自定义密码进行加密/解密,System.Security.Cryptography这里有一个使用命名空间中的某些类使用密码加密文件的示例(而不是硬编码)你可能会把它作为输入)http://www.codeproject.com/Articles/26085/File-Encryption-and-Decryption-in-C

请记住,正确执行安全措施非常困难