簡體   English   中英

使用C#寫入appData文件夾中的文件時,訪問被拒絕錯誤

[英]access denied error when writing into a file in appData folder using c#

我在C#中有一個桌面應用程序,它將數據存儲在XML文件中。 當應用程序安裝在“ Program Files”文件夾中時,其XML文件將轉到AppData(CommonApplicationData)文件夾中的文件夾。 從此文件讀取時沒有問題,但是在寫入文件時會進入對拒絕路徑的異常訪問 當文件位於除C:以外的其他驅動器中時,讀寫沒有問題。

下面是一個使目標路徑具有完全控制權限的類:

 public string ApplicationFolderPath
{
    get { return Path.Combine(CompanyFolderPath, applicationFolder); }
}
/// <summary>
/// Gets the path of the company's data folder.
/// </summary>
public string CompanyFolderPath
{
    get { return Path.Combine(directory, companyFolder); }
}


private string applicationFolder;
private string companyFolder;
private static readonly string directory =
    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

private void CreateFolders(bool allUsers)
{
    DirectoryInfo directoryInfo;
    DirectorySecurity directorySecurity;
    AccessRule rule;
    SecurityIdentifier securityIdentifier = new SecurityIdentifier
        (WellKnownSidType.BuiltinUsersSid, null);
    if (!Directory.Exists(CompanyFolderPath))
    {
        directoryInfo = Directory.CreateDirectory(CompanyFolderPath);
        bool modified;
        directorySecurity = directoryInfo.GetAccessControl();
        MessageBox.Show(directory.ToString());
        rule = new FileSystemAccessRule(
                securityIdentifier,
                FileSystemRights.FullControl,
                AccessControlType.Allow);
        directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
        directoryInfo.SetAccessControl(directorySecurity);
    }
    if (!Directory.Exists(ApplicationFolderPath))
    {
        directoryInfo = Directory.CreateDirectory(ApplicationFolderPath);
        if (allUsers)
        {
            bool modified;
            directorySecurity = directoryInfo.GetAccessControl();
            rule = new FileSystemAccessRule(
                securityIdentifier,
                FileSystemRights.Write |
                FileSystemRights.ReadAndExecute |
                FileSystemRights.Modify,
                InheritanceFlags.ContainerInherit |
                InheritanceFlags.ObjectInherit,
                PropagationFlags.InheritOnly,
                AccessControlType.Allow);
            directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
            directoryInfo.SetAccessControl(directorySecurity);
        }
    }
}
/// <summary>
/// Returns the path of the application's data folder.
/// </summary>
/// <returns>The path of the application's data folder.</returns>
public override string ToString()
{
    return ApplicationFolderPath;
}

現在在這里使用上面的類(CommonApplicationData)創建目標路徑:

        string _word, _path = new CommonApplicationData("Katek", "RemindWord", true).ToString() + @"\Words.xml", _description;

但是當插入Words.xml文件時,它給了我這個例外 在此處輸入圖片說明

用戶如何寫入文件?

問題在於file屬性(hidden) ,我已經將該文件隱藏了,以防止用戶意外刪除它(因為它就像應用程序的數據庫文件一樣,因此是一個至關重要的文件),但是在不隱藏的情況下對文件的讀寫沒有問題。 感謝您的全部貢獻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM