file write permission issue under Program Files folder
You should write user specific config data to the Application Data folder for the current user, using the special folders enum and the Enivronment.GetFolderPath.
Allow access permission to write in Program Files of Windows 7
Your program should not write temporary files (or anything else for that matter) to the program directory. Any program should use %TEMP% for temporary files and %APPDATA% for user specific application data. This has been true since Windows 2000/XP so you should change your aplication.
The problem is not Windows 7.
You can ask for appdata folder path:
string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
or for TEMP path
string dir = Path.GetTempPath()
Visual Studio, best way to compile directly to Program Files (access denied issues)?
I ended up running Visual Studio as an administrator. That was the simplest solution for me. Thanks everybody that contributed.
Security concerns for writing to application directory under Program Files
Most anti-virus programs will throw a fit if they see any updates under the program files directory.
You'd be better off writing your data to a folder under the AppData folder
Should I give my app write access to Program Files?
The answer, as stated by @PanagiotisKanavos in the comments, isn't to give write access to Program Files. The correct way to implement this is using the tempfiles
module of python.
import tempfile
def functionA():
temp_file = tempfile.TemporaryFile()
temp_file.write("hello world")
contents = temp_file.read()
temp_file.close()
def functionB():
temp_file = tempfile.NamedTemporaryFile()
file_path = temp_file.name
temp_file.close()
Note that tempfile deletes a temporary file when it is closed. If you intend to use the file on a windows platform it must be closed before it can be read by another process. To do this use the delete=False
flag when instantiating the TemporaryFile
object. Note that you will have to delete the file manualy. This is easily done by using a named temporary file and deleting the file at that path once you are done with it.
import tempfile
def functionC():
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write("hello world")
temp_path = temp_file.name
temp_file.close()
os.system(f"echo {temp_path}")
os.remove(temp_path)
Privileges/owner issue when writing in C:\ProgramData\
No, C:\ProgramData
, aka FOLDERID_ProgramData
, has restricted security settings. Standard users can create files there. But these files are, by default, secured so that only the user that created the file can subsequently modify the file.
The recommended solution is for your installer to create a sub directory of C:\ProgramData
for your shared storage. And that sub directory must be given a permissive ACL by the installation program. That is what grants the desired access to all standard users.
I do wonder whether you really need shared writeable data. Normally I'd expect to see shared configuration be something that is specified at install time and modified infrequently by administrators. Most configuration data tends to be per user.
Related Topics
Get Just the Domain Name from a Url
How to Change Originating Ip in Httpwebrequest
Patch Async Requests with Windows.Web.Http.Httpclient Class
Is There a Synchronization Class That Guarantee Fifo Order in C#
Xmlserializer Serialize Generic List of Interface
Http Error 500.19 When Publish .Net Core Project into Iis with 0X80070005
Regex Word Boundary Expressions
Circular Reference Causing Stack Overflow with Automapper
Parsing HTML to Get Content Using C#
Can a Picturebox Show Animated Gif in Windows Application
Datagridview Checkbox Column - Value and Functionality
Setting Dropdownlist Selecteditem Programmatically
Active Directory Com Exception - an Operations Error Occurred (0X80072020)
Show Transparent Loading Spinner Above Other Controls
How to Split an Ienumerable<String> into Groups of Ienumerable<String>
Why Does the Linq Cast<> Helper Not Work with the Implicit Cast Operator