简体   繁体   English

已安装的C#应用​​程序写入权限

[英]Installed C# application write permissions

I have a C# application that has been installed but this program needs to be able to read and write xml. 我已经安装了C#应用程序,但是该程序需要能够读取和写入xml。 I tested the installed program and it can read the xml if it is there but it gets an exception when the program closes. 我测试了已安装的程序,它可以读取xml(如果存在),但是在程序关闭时会出现异常。

Could someone help me. 有人可以帮我吗。

The current way I create xml is like this: 我创建xml的当前方法是这样的:

private void SaveQuickLinks(List<QuickLinksData> QuickLinks)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("QuickLinks.xml");
            doc.RemoveAll();


            XmlElement Root = doc.CreateElement("", "QuickLinksList", "");
            doc.AppendChild(Root);

            foreach (QuickLinksData QuickLink in QuickLinks)
            {
                //Create New QuickLink
                XmlElement LinkElement = doc.CreateElement("", "QuickLink", "");
                Root.AppendChild(LinkElement);

                //Create Element With QuickLink Name
                XmlElement QuickLinkName = doc.CreateElement("", "LinkName", "");
                XmlText LinkName = doc.CreateTextNode(QuickLink.Link_Name);
                QuickLinkName.AppendChild(LinkName);
                LinkElement.AppendChild(QuickLinkName);

                //Create Element with QuickLink Link
                XmlElement QuickLinkURL = doc.CreateElement("", "LinkURL", "");
                XmlText LinkURL = doc.CreateTextNode(QuickLink.Link_URL);
                QuickLinkURL.AppendChild(LinkURL);
                LinkElement.AppendChild(QuickLinkURL);
            }

            //File.SetAttributes("QuickLinks.xml", FileAttributes.Normal);
            //doc.Save("QuickLinks.xml");

            using (var file = new FileStream("QuickLinks.xml", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                doc.Save("QuickLinks.xml");
            }
        }

I need the be able to over write this every time my application closes. 每当我的应用程序关闭时,我都需要能够重写此内容。

Every C# program has the permissions of the user that is executing it, it is not managed on the application level. 每个C#程序都具有执行它的用户的权限,而不是在应用程序级别上进行管理。 Check that the user that executes the program has write access to the location where you are saving the file to (which seems to be the working directory of the program - usually the folder where the binaries are, but that might be different). 检查执行该程序的用户是否具有将文件保存到的位置(这似乎是该程序的工作目录,通常是二进制文件所在的文件夹,但是可能有所不同)的写访问权限。

Eg if you installed it to C:\\Programme Files , and are running it as a normal user, then it might not have write access to this directory (you need Admin rights by default). 例如,如果将其安装到C:\\Programme Files并以普通用户身份运行,则它可能对该目录没有写权限(默认情况下,您需要管理员权限)。 Consider moving the file to eg %APPDATA% 考虑将文件移动到例如%APPDATA%

Check the access permissions and grant write permissions to users accessing this file. 检查访问权限,并向访问此文件的用户授予写权限。 When you install c# application by default it goes to C:\\Program Files\\'Your application' and it is not a good practice to create/overwrite files present under C:\\Program Files. 默认情况下,当您安装c#应用程序时,转到C:\\ Program Files \\'您的应用程序' ,创建/覆盖C:\\ Program Files下的文件不是一个好习惯。

I have faced this issue and came up with this solution for my application 我已经遇到了这个问题,并为我的应用提出了这个解决方案

  1. Create a directory with application name under your user profile ( C:\\Users\\'Application Name' for windows 7). 在用户配置文件下创建一个带有应用程序名称的目录(对于Windows 7为C:\\ Users \\'Application Name' )。 User will have full access to his user profile 用户将拥有其用户资料的完全访问权限
  2. Place this file in that directory and get the file path using the following code 将此文件放在该目录中,并使用以下代码获取文件路径

    string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Application Name"), "File Name"); 字符串filePath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),“应用程序名称”),“文件名”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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