简体   繁体   English

使用使用C#开发的Windows Service锁定和解锁文件夹

[英]Lock and Unlock a folder using windows service developed using c#

I know similar question has been asked many times , but nothing is matching with my requirement. 我知道类似的问题已经问过很多次了,但是没有任何一项与我的要求相符。 If it matches, please feel free to direct me to the right answer. 如果匹配,请随时将我引向正确的答案。

I have a windows service developed in c# , .net framework 4.6. 我有一个用c#,.net framework 4.6开发的Windows服务。
This windows service receives a task every 1 hr from other service (which is of no importance in this question).It actually processes the task which it receives .It takes around 10-15 mins for the execution to complete for every task. Windows服务每隔1个小时就会从其他服务接收一次任务(此问题无关紧要)。它实际上处理接收到的任务。每个任务的执行大约需要10-15分钟才能完成。 During the execution of the task , it generates multiple files in one the folders in the server. 在执行任务期间,它会在服务器的一个文件夹中生成多个文件。
In App.config, I have the folderpath saved. 在App.config中,我保存了folderpath。 Say for example I have appsetting whose value is "C:\\Test". 举例来说,我的appsetting的值为“ C:\\ Test”。
Every task has a taskid, say for example it is Task1. 每个任务都有一个taskid,例如说Task1。 So we create a folder like "C:\\Test\\Task1". 因此,我们创建了一个文件夹,例如“ C:\\ Test \\ Task1”。 All the files get generated here. 所有文件都在这里生成。 And as soon as the execution is finished , this folder gets deleted. 并且一旦执行完成,该文件夹就被删除。

Now my requirement is from the time it receives a task , till the execution is completed, I have to lock the folder where these files are getting generated. 现在,我的要求是从接收任务开始到执行完成为止,我必须锁定生成这些文件的文件夹。
I need to unlock the folder as soon as the execution is finsihed.By lock I mean , no one should be able to open this folder. 完成执行后,我需要立即解锁该文件夹。通过锁定,我的意思是,没有人应该能够打开该文件夹。

I know the files can be locked. 我知道文件可以被锁定。 But how to lock the folder here. 但是如何在这里锁定文件夹。 Can any one help me here or direct me the right article . 任何人都可以在这里帮助我或为我推荐正确的文章。
It would be of much help.Many thanks! 这会很有帮助。非常感谢!

This is possible, and I think there is more than one method to do this. 这是可能的,我认为有多种方法可以做到这一点。

First, you have to create a temporary windows account . 首先,您必须创建一个临时windows account Why? 为什么? To allow access to this specified directory for only one user and removing access to other accounts. 仅允许一个用户访问此指定目录,并删除对其他帐户的访问。 You can do this by using DirectorySecurity class available in the System.Security.AccessControl namespace. 您可以使用System.Security.AccessControl命名空间中提供的DirectorySecurity类来完成此操作。

There's also an example attached to it. 上面还附有一个示例。

PS: A similar post is available at msdn PS: msdn上有类似的帖子

Locking the files on disk is something that's difficult to do/get right. 将文件锁定在磁盘上是很难做到的事情。 One alternative you may wish to consider is to instead use Memory-Mapped files , specifically non-persisted memory-mapped files : 您可能要考虑的一种替代方法是改用Memory-Mapped文件 ,特别是非持久性 Memory-Mapped 文件

Non-persisted files are memory-mapped files that are not associated with a file on a disk. 非持久性文件是与磁盘上的文件不关联的内存映射文件。 When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. 当最后一个进程处理完文件后,数据将丢失,并且垃圾回收将回收该文件。 These files are suitable for creating shared memory for inter-process communications (IPC). 这些文件适用于为进程间通信(IPC)创建共享内存。

Here's a very short example that shows creating a non-persisted memory-mapped file, writing to it and reading from it: 这是一个非常简短的示例,显示了如何创建一个非持久性的内存映射文件,对其进行写入和读取:

static void Main(string[] args)
{
    var file = CreateFile("Mary had a little lamb, its fleece was white as snow...");

    var contentFromFile = ReadFile(file);

    Console.WriteLine(contentFromFile);
}

private static MemoryMappedFile CreateFile(string content)
{
    var file = MemoryMappedFile.CreateNew("temp.csv", 1048576);

    using (var stream = file.CreateViewStream())
    {
        using (var writer = new StreamWriter(stream))
        {
            writer.WriteLine(content);
        }
    }

    return file;
}

private static string ReadFile(MemoryMappedFile file)
{
    using (var stream = file.CreateViewStream())
    {
        using (var reader = new StreamReader(stream))
        {
            var contentReadFromStream = reader.ReadLine();
            return contentReadFromStream;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM