Reputation: 3
I have an application that needs to create, read and delete 2 xml files. These files are stored in the same directory as the application file. When I run this application in debug it works fine. but as soon as you create an installer for this and run the installed program it failed to create the files. Is there any way I can set the application permission to the folder it has been installed to?
Any help would be great.
Thank you
Here is the code im using:
if (File.Exists("ApplicationData.xml") == true)
{
File.SetAttributes("ApplicationData.xml", FileAttributes.Normal);
File.Delete("ApplicationData.xml");
}
doc.Save("ApplicationData.xml");
This is throwing a System.UnauthorizedAccessException
Upvotes: 0
Views: 98
Reputation: 25154
I would suggest you to create a "tmp" folder from your application, and then create your xml files inside . This will guarantee you that have permission on the folders, as you have created them in the privilege scope of the application.
string folderName = @"Folder";
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
System.IO.Directory.CreateDirectory(pathString);
Here i've tested it out for you: http://pastebin.com/cAmFQvee
This works fine
Upvotes: 0
Reputation: 62532
Rather than start changing directory permissions, how about putting the files into the temporary directory? You can get the directory from Path.GetTempPath()
.
Upvotes: 1