简体   繁体   English

WPF应用程序启动行为

[英]WPF App launch behavior

I developed a C# WPF App. 我开发了一个C#WPF应用程序。 Running it in the VS17 debugger - all fine 在VS17调试器中运行它 - 一切正常

Double click the .exe in the release folder - all fine 双击release文件夹中的.exe - 一切正常

I deployed the software on multiple devices, double click the .exe - all fine 我在多个设备上部署了该软件,双击.exe - 一切都很好

Starting the deployed App via powershell with Start-Process -FilePath "C:\\Users\\krueperj\\Desktop\\Release\\PrometheusStartup.exe" - all fine 使用Start-Process -FilePath "C:\\Users\\krueperj\\Desktop\\Release\\PrometheusStartup.exe"通过powershell启动已部署的应用程序 - 一切都很好

Now it gets weird. 现在它变得奇怪了。
I wanted to start the App on logon. 我想在登录时启动App。 When I created a task in Windows Task Scheduler the App won't start because of an exception. 当我在Windows任务计划程序中创建任务时,应用程序将因异常而无法启动。 When I schedule the powershell (which opens the .exe) and it starts, the App won't start because of the same exception. 当我安排powershell(打开.exe)并启动时,应用程序将因为相同的异常而无法启动。 When I put my App through code into the registry to autostart, same exception. 当我通过代码将我的应用程序放入注册表中以自动启动时,同样的异常。

Here is what eventlog says: 以下是eventlog所说的内容:

System.UnauthorizedAccessException bei System.IO.__Error.WinIOError(Int32, System.String) bei 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, Boolean) bei System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions, System.String, Boolean, Boolean, Boolean) bei System.IO.File.InternalWriteAllBytes(System.String, Byte[], Boolean) bei System.IO.File.WriteAllBytes(System.String, Byte[]) bei PrometheusStartup.FileCache.AfterAccessNotification(Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCacheNotificationArgs) bei Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache.OnAfterAccess(Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCacheNotificationArgs) bei Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.NotifyAfterAccessCache System.UnauthorizedAccessException bei System.IO .__ Error.WinIOError(Int32,System.String)bei 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,Boolean)bei System.IO.FileStream..ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System .IO.FileShare,Int32,System.IO.FileOptions,System.String,Boolean,Boolean,Boolean)bei System.IO.File.InternalWriteAllBytes(System.String,Byte [],Boolean)bei System.IO.File.WriteAllBytes (System.String,Byte [])bei PrometheusStartup.FileCache.AfterAccessNotification(Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCacheNotificationArgs)bei Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache.OnAfterAccess(Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCacheNotificationArgs)bei Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.NotifyAfterAccessCache () bei Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase+d__53.MoveNext() bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) bei Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext+d__48.MoveNext() bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) bei Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions+d__0.MoveNext() bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) bei PrometheusStartup.MainWindow+d__18.MoveNext() bei System ()bei Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase + d__53.MoveNext()bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System。 Threading.Tasks.Task)bei Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext + d__48.MoveNext()bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)bei System.Runtime.CompilerServices.TaskAwaiter。 HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)bei Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions + d__0.MoveNext()bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)bei System.Runtime。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)bei PrometheusStartup.MainWindow + d__18.MoveNext()bei System .Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0(System.Object) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) bei System.Windows.Threading.DispatcherOperation.InvokeImpl() bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) bei MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, Syst .Runtime.CompilerServices.AsyncMethodBuilderCore + <> c.b__6_0(System.Object)bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object,Int32)bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System。 Object,System.Delegate,System.Object,Int32,System.Delegate)bei System.Windows.Threading.DispatcherOperation.InvokeImpl()bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)bei System.Threading.ExecutionContext。 RunInternal(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)bei System。 Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object)bei MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext,System.Threading.ContextCallback,Syst em.Object) bei System.Windows.Threading.DispatcherOperation.Invoke() bei System.Windows.Threading.Dispatcher.ProcessQueue() bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) bei MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) bei MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) bei System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.Dispa em.Object)bei System.Windows.Threading.DispatcherOperation.Invoke()bei System.Windows.Threading.Dispatcher.ProcessQueue()bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef) bei MS.Win32.HwndWrapper.WndProc(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef)bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System。对象,Int32)bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate,System.Object,Int32,System.Delegate)bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading。 DispatcherPriority,System.TimeSpan,System.Delegate,System.Object,Int32)bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)bei MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef )bei System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.Dispa) tcherFrame) bei System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) bei System.Windows.Application.RunDispatcher(System.Object) bei System.Windows.Application.RunInternal(System.Windows.Window) bei System.Windows.Application.Run(System.Windows.Window) bei PrometheusStartup.App.Main() tcherFrame)bei System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)bei System.Windows.Application.RunDispatcher(System.Object)bei System.Windows.Application.RunInternal(System.Windows.Window)bei System.Windows.Application.Run(System.Windows.Window)bei PrometheusStartup.App.Main()

You may want to make a batch file to kick off your PowerShell script. 您可能希望制作批处理文件以启动PowerShell脚本。 Run it as administrator. 以管理员身份运行它。 You will need to set the execution policy as well. 您还需要设置执行策略。 Here's the code: 这是代码:

Powershell.exe -executionpolicy remotesigned -Command "& 'yourthing.ps1'"

The advantage of doing this is you don't have to start up PowerShell ISE as administrator, or PowerShell as administrator and execute your script. 这样做的好处是您不必以管理员身份启动PowerShell ISE,也不必以管理员身份启动PowerShell并执行脚本。 All you do is right click the batch file and run as administrator. 您只需右键单击批处理文件并以管理员身份运行。 Easy! 简单!

One last thing. 最后一件事。 With Windows Task Scheduler you will have to check the properties of whatever you choose to run (your current way, just the PowerShell script, my suggested batch file, or just the .exe) and make sure the "run with highest privilege" is on. 使用Windows任务计划程序,您将必须检查您选择运行的任何属性(您当前的方式,只是PowerShell脚本,我建议的批处理文件,或只是.exe)并确保“以最高权限运行”已启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM