无法以标准用户身份创建或写入文件
我的应用程序在连接或断开服务器时创建一个日志文件。这在 win XP 下效果很好。
当我在 Vista 和 Win 7 中以管理员身份运行它时,没有任何问题。但如果我在 Vista 或 Win7 中以标准用户身份运行它,则它无法创建日志文件,而且我知道这是针对 UAC 的。
但我怎样才能绕过这种情况,以便我的应用程序将以 admin 身份运行。我需要在安装应用程序时添加任何注册表项,以便它始终以管理员身份运行吗?
My application creates a log file when it connects or disconnects a server. This works well win win XP.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确实愿意,可以 添加一个清单,使您的应用程序在启动时需要 UAC 提升,但通常情况下不会那不是你应该做的。
更有可能的是,你正在写一条不应该写的路。用户模式应用程序应仅写入当前用户文件夹或
C:\Users\Public
文件夹,日志等内容应放入C:\Users\Current User Name\Application Data\您的程序名称\
。要获取 ApplicationData 路径,请使用Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
。或者,如果您希望所有用户共享该文件,请使用Environment.SpecialFolder.CommonApplicationData。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.
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 inC:\Users\Current User Name\Application Data\Your Program Name\
. To get the ApplicationData path useEnvironment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
. Or if you want to have the file shared by all users, useEnvironment.SpecialFolder.CommonApplicationData
.