应用程序是否需要父文件夹的写权限?

时间:2017-05-09 16:24:45

标签: c# .net

当我尝试在当前用户没有写入权限的文件夹中运行应用程序时,应用程序崩溃。

所以我的问题是,.net应用程序是否默认创建父文件夹中的临时文件?

如果是,我们是否可以显式设置一个路径来设置一个外部文件夹,其中应用程序具有写入权限以创建其临时文件?

由于

编辑:我回来了更多细节...... 因此异常是System.UnauthorizedAccessException,它尝试创建的文件名为“l1m5tzj4.tmp”

所以我现在确定.NET应用程序正在尝试在当前文件夹中创建一个临时文件...

这是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)

编辑2:所以当我在exe配置文件中添加一个新值时,它看起来就好了。

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (configuration.AppSettings.Settings[key] != null)
{
    configuration.AppSettings.Settings[key].Value = value;
    configuration.Save();
    ConfigurationManager.RefreshSection("appSettings");
}

那么为什么我在尝试保存配置时尝试创建临时文件呢? 当我们没有对其文件夹的写入权限时,是否有保存配置的最佳实践?

1 个答案:

答案 0 :(得分:5)

不,默认情况下,.NET应用程序不会在运行时在包含正在执行的程序集的文件夹中创建任何(临时或其他)文件。

可以做到这一点的机制包括:

  • 跟踪
  • 登录
  • 库的人工制品,例如ODP.NET数据库提供者
  • 动态编译,例如,序列化程序

编辑到目前为止,还有其他可用信息,因此我们也可以在此处解决您的实际情况:

您调用Configuration.Save,它会尝试更新 filename.exe.config 文件。现在你的问题是为什么系统,而不是只是覆盖现有的配置文件,创建一个临时文件

原因是一位不知名的微软程序员给出了评论:

  

当前算法保证
   1)我们保存的文件将始终存在       在文件系统上(即没有窗口       在保存期间,那里没有文件)
   2)它总是可以从a读取       客户端,它将是完整和准确的。

首先,他们创建一个临时文件here,其中写入新配置。这是您在程序抛出异常时看到失败的原因。

在此之后,他们用临时文件替换原始配置文件(在您的情况下执行永远不会那么远),here

  

当我们没有对其文件夹的写入权限时,是否有保存配置的最佳做法?

您可以将配置文件重定向到您选择的任何其他文件夹:
Relocating app.config file to a custom path
Accessing App.config in a location different from the binary