代码之家  ›  专栏  ›  技术社区  ›  jp2code

适用于任何用户、C#、Windows的安全存储位置

  •  0
  • jp2code  · 技术社区  · 16 年前

    我为我们公司准备了一个应用程序,它必须以全球方式存储设置,这样拥有多种访问类型的员工就可以运行该程序。

    因此,我似乎找不到方法让我的应用程序将数据写入PC。错误总是相同的未授权访问异常。

    我尝试过使用以下路径,但都失败了:

    • C:\程序套件\
    • 环境.GetFolderPath(环境.SpecialFolder.ApplicationData)
    • 环境.GetFolderPath(环境.SpecialFolder.CommonApplicationData)

    我不想使用独立存储,因为我不想为每个登录到我们工厂的许多PC中的一台的个人配置应用程序。

    有没有办法说,“嘿,我的应用程序是管理员。给我权限!“???

    我们目前仅限于.NET Framework 3.5。

    :

    异常消息:

    System.UnauthorizedAccessException: Access to the path 'C:\Documents and Settings\All Users\Application Data\Aaon Coil Products, Inc\AcpConfig.log' is denied.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
       at Suite.MdiForm.SaveFormSettings()
       at Suite.MdiForm.MdiForm_Closing(Object sender, FormClosingEventArgs e)
       at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
       at System.Windows.Forms.Form.WmClose(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3603 (GDR.050727-3600)
        CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    Suite
        Assembly Version: 2.2.8.28978
        Win32 Version: 2.2.8
        CodeBase: file:///C:/Program%20Files/Aaon%20Coil%20Products,%20Inc/ACP%20Software%20Suite/Suite.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Core
        Assembly Version: 3.5.0.0
        Win32 Version: 3.5.30729.1 built by: SP
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.Management
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Management/2.0.0.0__b03f5f7f11d50a3a/System.Management.dll
    ----------------------------------------
    System.Data
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.DirectoryServices
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.DirectoryServices/2.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------
    System.EnterpriseServices
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
    ----------------------------------------
    AcpSuiteFormHeader
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0
        CodeBase: file:///C:/Program%20Files/Aaon%20Coil%20Products,%20Inc/ACP%20Software%20Suite/AcpSuiteFormHeader.DLL
    ----------------------------------------
    
    4 回复  |  直到 16 年前
        1
  •  1
  •   Wil P    16 年前

    我使用CommonApplicationData,它似乎可以正常工作,但有一点需要注意。我发现如果管理员在CommonApplicationData路径中创建子目录,那么其他“非管理员”用户就不能删除该目录。您可能需要添加 FileSystemAccessRule

    编辑:使用代码段。

    public static void CreateWithEveryoneFullControlIfAdmin(this DirectoryInfo source)
    {
        if (IsAdmin())
        {
            DirectorySecurity directorySecurity = Directory.GetAccessControl(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
            FileSystemAccessRule accessRule
                = new FileSystemAccessRule(@"BUILTIN\Users", FileSystemRights.FullControl,
                    InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow);
    
            bool modified = false;
            directorySecurity.ModifyAccessRule(AccessControlModification.Add,
                accessRule,
                out modified);
    
            if (modified)
            {
                source.Create(directorySecurity);
            }
            else
            {
                source.Create();
            }
        }
        else
        {
            source.Create();
        }
    }
    
    public static bool IsAdmin()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    
        2
  •  2
  •   Zippit    16 年前

    我知道你说过你不想使用IsolatedStorage,因为你不想每个用户都有自己的设置。

    但是你看过使用机器存储的隔离存储吗?如果我理解正确(我可能是错的,这不是第一次),这应该是所有访问你的应用程序的用户共享的。

    using(IsolatedStorageFile store = IsolatedStorageFile.GetMachineStoreForApplication())
    {
        // check if file exists or not
        try
        {
            using(IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(STG_FILE_NAME, FileMode.OpenOrCreate, store)) {
                StreamWriter sw = new StreamWriter(isfs);
                foreach(string key in m_values.Keys) {
                    sw.WriteLine(key + "::" + m_values[key]);
                } // foreach
                sw.Flush();
            } // using
        } catch(IOException) {
            // generally because file is locked by another process...do nothing
        }
    } // using
    

    或者,您可以更改应用程序配置文件的权限以允许所有人访问。这可以由特权用户在应用程序第一次运行时完成。

    http://www.techtalkz.com/c-c-sharp/153732-saving-files-so-any-user-can-access-them.html

    FileInfo fileInfo = new FileInfo(path);
    FileSecurity fileSecurity = fileInfo.GetAccessControl();
    fileSecurity.AddAccessRule(new FileSystemAccessRule(
        "Users",
        FileSystemRights.FullControl,
        AccessControlType.Allow) );
    
    fileInfo.SetAccessControl(fileSecurity);
    
        3
  •  1
  •   egrunin    16 年前

        4
  •  1
  •   Aliostad    16 年前

    好的,这是我的两分钱,希望对你有帮助。

    如果可以在程序中硬编码一些管理级凭据,则可以对需要访问文件夹的代码部分使用模拟。请看这里:

    http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

    推荐文章