C# - 锁定正在编辑的文件夹 [英] C# - Locking folders from being edited

查看:126
本文介绍了C# - 锁定正在编辑的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小项目。它是Windows窗体应用程序。我在文件夹中找到了一些源代码(C:/ sources)。当我的程序运行时,它使用该文件夹中的源代码。目前我可以通过Windows资源管理器编辑文件夹,它可能会导致我的程序错误。所以我想在我的程序运行时锁定文件夹(C:/ sources)被编辑/重命名/删除。怎么做?



编辑;
当用户试图编辑文件夹时,是否可以显示这样的消息:



操作无法完成,因为该文件夹或文件位于它在另一个程序中打开



我们正在谈论的程序是我的。

解决方案

有几种方法可以冒险,它们的实施难度各不相同。这完全取决于这项任务对你有多重要。但是,在讨论这些选择之前,你不能将这些资源嵌入到你的WinForms应用程序中吗?如果这不是一个选项,那么您可以执行以下任一操作:


  1. 编写一个设备驱动程序,可以禁止访问这些资源如果您的应用程序正在运行这种方法有一些回退。例如,可以通过具有相同名称来模拟您的应用程序。但是,当我试图解决当前问题的可能解决方案时,我并没有过多地尝试打破任何方法。您可以考虑不同类型的驱动程序。可能这种方法最简单的形式是实现一个微型过滤器驱动程序。

  2. code>, NtCreateFile() ZwCreateFile()尽管有很多方法来规避这种防御机制。但是,我们只是冒险尝试解决您的这个限制。 直接从应用程序打开这些资源并专门锁定它。例如:

    File.Open(test.txt,FileMode.Open,FileAccess.Read,FileShare.None);

    ,因为如果他们尝试打开文件,会导致用户收到你想要的信息。 b
    也许你可以提供更多关于这些资源的信息,并且我们可以帮助你确定哪种方法是以合理的方式保护你的文件的最佳方式?

    i'm making a small project. it's Windows Form Application. i got some sources in a folder (C:/sources). When my program runs, it uses sources from the folder. Currently i can edit the folder by windows explorer, it can cause errors for my program. So i want to lock the folder (C:/sources) from being edited/renamed/deleted when my program runs. How to do so?

    EDIT; Is it possible to show a message like this when user has tried to edit the folder:

    "the action cannot be completed because the folder or a file in it is open in another program"

    the program that we are talking about is mine..

    解决方案

    There are a couple of approaches that you could venture and they vary in difficulty of implementation. It all depends on how important this task is for you. But, before discussing these options; can't you embed those resources in your WinForms application instead? If this is not an option then you can do one of the following:

    1. Write a device driver that can prohibit the access of such resources if your application is running. There are fallbacks to this approach. For example one can impersonate your application by having the same name. But, am not getting in to too much details in trying to break any approach as I am trying to address possible solutions to the current problem. There are different types of drivers that you can consider. Probably the simplest form of this approach would be to implement a mini-filter driver.

    2. Hook certain API's like CreateFile(), NtCreateFile(), ZwCreateFile() although there are many ways to circumvent such mechanism of defense. But, again we are only venturing what you can do to address this constraint of yours.

    3. Open these resources directly from your application and lock it exclusively. For example:
      File.Open("test.txt", FileMode.Open, FileAccess.Read, FileShare.None);
      as this will result in people getting the message that you desire if they try to open the file.

    Maybe you can give more information on what these resources are and we can help you determine which is the best way to protect your files in a reasonable fashion?

    这篇关于C# - 锁定正在编辑的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆