简体   繁体   中英

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.
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. 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. Say for example I have appsetting whose value is "C:\\Test".
Every task has a taskid, say for example it is Task1. So we create a folder like "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 . 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.

There's also an example attached to it.

PS: A similar post is available at 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 :

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).

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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