Does an application require write permission on the parent folder?

.net, c#

Solution

No, a .NET application does not by default create any (temporary or other) files in the folder that contains the executing assembly at runtime.

Mechanisms which could do that are, among others:

- Tracing

- Logging

- Artefacts of libraries like for example the ODP.NET database provider

- Dynamic compilation of, for example, serializers

EDIT By now, there is additional information available, so we can also address your actual scenario here:

You call `Configuration.Save`, which attempts to update the filename.exe.config file. Now your question is why the system, instead of just overwriting the existing config file, creates a temporary file.

The reason is given by an unknown Microsoft programmer, who commented:

The current algorithm guarantees 1) The file we are saving to will always be present on the file system (ie. there will be no window during saving in which there won't be a file there) 2) It will always be available for reading from a client and it will be complete and accurate.

First, they create a temporary file, here, where the new configuration is written to. This is what you see failing when your program throws the exception.

After this, they replace the original configuration file with the temporary file (execution never gets that far in your case), here.

And is there any best practices to save configurations when we don't have write access to its folder?

You are free to redirect the configuration file to any other folder of your choice: Relocating app.config file to a custom path Accessing App.config in a location different from the binary

Problem

The application crashes when I try to run it inside a folder where the current user has no write access to it. So my question is, does a .net application create by default temporary files in the parent folder ? And if yes, can we explicitly set a path to set an external folder where the application has write access to create its temp files ? Thanks EDIT: I'm back with more details ... So the exception is a System.UnauthorizedAccessException, and the file it tries to create is named "l1m5tzj4.tmp" So I'm now sure .NET application is trying to create a temporary file in the current folder ... Here is the stacktrace ``` 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, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.CodeDom.Compiler.TempFileCollection.EnsureTempNameCreated() at System.CodeDom.Compiler.TempFileCollection.get_BasePath() at System.CodeDom.Compiler.TempFileCollection.AddExtension(String fileExtension, Boolean keepFile) at System.CodeDom.Compiler.TempFileCollection.AddExtension(String fileExtension) at System.Configuration.Internal.WriteFileContext..ctor(String filename, String templateFilename) at System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForWrite(String streamName, String templateStreamName, Object& writeContext, Boolean assertPermissions) at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForWrite(String streamName, String templateStreamName, Object& writeContext, Boolean assertPermissions) at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForWrite(String streamName, String templateStreamName, Object& writeContext) at System.Configuration.ClientConfigurationHost.OpenStreamForWrite(String streamName, String templateStreamName, Object& writeContext) at System.Configuration.UpdateConfigHost.OpenStreamForWrite(String streamName, String templateStreamName, Object& writeContext) at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll) ``` EDIT 2: So it looks like it happens while I'm putting a new value in exe config file. ``` Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (configuration.AppSettings.Settings[key] != null) { configuration.AppSettings.Settings[key].Value = value; configuration.Save(); ConfigurationManager.RefreshSection("appSettings"); } ``` So why it tries to create a temporary file while I'm trying to save the configuration ? And is there any best practices to save configurations when we don't have write access to its folder ?

Original source

Related problems