Windows登录结果开始于System.UnauthorizedAccessException [英] Start on windows logon results in System.UnauthorizedAccessException

查看:149
本文介绍了Windows登录结果开始于System.UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Windows窗体应用程序.在运行表单之前,我将启动时间写到日志文本文件中.这是通过Program.Main()方法完成的.
当我运行该程序时,一切正常.

但是现在我想在Windows登录上启动该应用程序.因此,我在LM/Software/Microsoft/Windows/Currentversion/Run中编写了应用程序的path + exe-name.但是,当我登录到Windows时,抛出了带有以下堆栈跟踪的UnauthorizedAccessException:

I have written a windows forms application. Before I run the form, I write the startup time to a log textfile. This is done in the Program.Main() method.
When I run the program, everything works perfect.

But now I want to start the application on windows logon. So I have written the path+exe-name of the application in LM/Software/Microsoft/Windows/Currentversion/Run. But when I log on to windows, an UnauthorizedAccessException is thrown with the following stack trace:

<small>System.IO.__Error.WinIOError(Int32, System.String)
System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)
System.IO.StreamWriter.CreateFile(System.String, Boolean)
System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32)
System.IO.StreamWriter..ctor(System.String, Boolean</small>



有人可以告诉我我在这里缺少什么吗?



Can anyone tell me what I am missing here?

推荐答案

好像您无法使用此登录名访问文件.
确保此登录名可以访问目录路径.
Looks like you don''t have access to the file using this login.
Make sure this login has access to the directory path.


您提到的注册表项(HKLM/Software/Microsoft/Windows/CurrentVersion/Run)在Windows启动时启动应用程序,因此时间还没有用户登录,并且您的应用程序没有正确的凭据来执行所需的写入操作.

如果要在特定用户登录Windows时启动程序,请使用注册表项HKCU/Software/Microsoft/Windows/CurrentVersion/Run.

要让所有登录到计算机的用户启动应用程序时,可以使用所有用户启动"文件夹.
The registry key you mentioned (HKLM/Software/Microsoft/Windows/CurrentVersion/Run) starts applications when windows is started, so at that time no user has logged on yet and your application does not have the right credentials to perform the write action you want.

If you want the program to start when a specific user logs onto Windows use the registry key HKCU/Software/Microsoft/Windows/CurrentVersion/Run.

You could use the All Users Startup folder when you want the application to start for all users who logon to a machine.


您的应用程序是否使用相对路径打开或创建文件?
如果是这样,则问题的原因是当前目录不同.

如果您自己运行该应用程序,则当前目录是该应用程序所在的目录.

如果您使用HKLM \ ... \ Run或HKCU \ ... \ Run,则当前目录是您的登录帐户的根目录.

解决方案:在访问任何文件之前设置适当的当前目录,或者始终使用绝对路径.
Does your app open or create a file using relative path?
If so, the cause of the problem is a difference of current directory.

If you run the app yourself, the current directory is the directory where the app is located.

If you use either HKLM\...\Run or HKCU\...\Run the current directory is the root of your login account.

The solution: set a proper current directory before accessing any file or always use absolute path.


这篇关于Windows登录结果开始于System.UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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