【问题标题】:How to create a Folder and read write restrict it, so that it could be open through my application only?如何创建文件夹并对其进行读写限制,使其只能通过我的应用程序打开?
【发布时间】:2019-07-21 17:49:03
【问题描述】:

我创建了一个文件夹,现在我想限制它,这样任何人都无法在其中读取或写入。此文件夹只能由我的应用程序访问。我只能通过我的应用程序阅读、写入它。实际上我想在我的文件夹中部署一个沙箱。

private void cFolder(object sender, RoutedEventArgs e)
{
    try
    {

        if (!Directory.Exists(pathTextBox.Text + "\\" + nameTextBox.Text))
        {
            //main Folder
            Directory.CreateDirectory(pathTextBox.Text + "\\" + nameTextBox.Text);

            //drive name Folder
            string subdirectory = System.IO.Path.Combine(pathTextBox.Text + "\\" + nameTextBox.Text, "C");
            System.IO.Directory.CreateDirectory(subdirectory);

            //creating sub folders
            string subfolder1 = System.IO.Path.Combine(subdirectory, "Program Files");
            System.IO.Directory.CreateDirectory(subfolder1);
            string subfolder2 = System.IO.Path.Combine(subdirectory, "Program Files(x86)");
            System.IO.Directory.CreateDirectory(subfolder2);
            string subfolder3 = System.IO.Path.Combine(subdirectory, "Setup");
            System.IO.Directory.CreateDirectory(subfolder3);

            //Permissions call


            //next window
            applicationImport go = new applicationImport(pathTextBox.Text, nameTextBox.Text);
            this.Hide();
            go.Show();
        }
        else
        {
            MessageBox.Show("Directory Exists Already", "Error creating folder");
        }
    }
    catch
    {
        MessageBox.Show("Error creating directory. Cannot Access Location");
    }
}

【问题讨论】:

    标签: c# wpf directory


    【解决方案1】:

    我知道限制对文件夹的访问的唯一方法是通过 NTFS 权限。不过,这是每个用户,而不是每个应用程序。如果您希望用户无法找到该文件夹​​,您可以将其放在用户的 AppData 文件夹中。此文件夹对大多数用户是隐藏的,除非在“文件夹选项”对话框中选择了“显示隐藏的文件和文件夹”选项并且取消选择了“隐藏受保护的操作系统文件(推荐)”选项。要在 C# 中使用它,请使用 Environment.SpecialFolder.ApplicationData 作为位置。下面是一个示例,使用您的代码:

    //creating sub folders
                string subfolder1 = System.IO.Path.Combine(Environment.SpecialFolder.ApplicationData,"MyApplication");
                System.IO.Directory.CreateDirectory(subfolder1);
                }
    

    这将创建一个位于C:\Users\<username>\AppData\Roaming\MyApplication 的文件夹 如果Environment.SpecialFolder.ApplicationData 不起作用,您可以尝试其他Environment.SpecialFolderloc​​ations。我自己不知道该怎么做,但是在服务帐户而不是用户帐户的上下文中运行应用程序可能会起作用,然后将文件夹的权限分配给该服务帐户-请参阅以下链接https://superuser.com/questions/592216/restrict-access-to-a-folder-by-exe .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 2014-10-22
      • 2022-01-11
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多