无法以标准用户的身份创建或写入文件 [英] Unable to Create or Write to a file as standard user

查看:206
本文介绍了无法以标准用户的身份创建或写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在连接或断开服务器时会创建日志文件。

My application creates a log file when it connects or disconnects a server. This works well win win XP.

在Vista和Win 7中以管理员身份运行它没有问题。但是如果我在Vista中运行这个标准用户, Win7然后它无法创建日志文件,我知道这是为UAC。

There are no issues while I run it as admin in Vista and Win 7. But if I run this as a standard user in Vista or Win7 then it fails to create the log file, and I know this is for UAC.

但是如何绕过这种情况,使我的应用程序将作为管理员运行。我需要在安装我的应用程序时添加任何注册表项,以便它始终以管理员身份运行。

But how can I bypass this situation so that my app will run as admin . DO I need to add any registry entry while installing my application so that it will always run as administrator.

推荐答案

您可以添加清单,导致您的应用程序在启动时需要UAC升级,但更多的时候,应该这样做。

If you really want to, you can add a manifest that causes your application to require UAC elevation when it is launched, but more often than not that's not what you should do.

更有可能的是,你写的路径你不应该。用户模式应用程序应该只写入当前用户文件夹或 C:\Users\Public 文件夹,日志应该放在 C :\Users \Current User Name \Application Data\您的计划名称\ 。要获取ApplicationData路径,请使用 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)。或者,如果要让所有用户共享该文件,请使用 Environment.SpecialFolder.CommonApplicationData

More likely, you're writing to a path you shouldn't be. User mode applications should only write to the current users folders or the C:\Users\Public folder, and things like logs should go in C:\Users\Current User Name\Application Data\Your Program Name\. To get the ApplicationData path use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData). Or if you want to have the file shared by all users, use Environment.SpecialFolder.CommonApplicationData.

这篇关于无法以标准用户的身份创建或写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆