简体   繁体   中英

WPF App launch behavior

I developed a C# WPF App. Running it in the VS17 debugger - all fine

Double click the .exe in the release folder - all fine

I deployed the software on multiple devices, double click the .exe - all fine

Starting the deployed App via powershell with Start-Process -FilePath "C:\\Users\\krueperj\\Desktop\\Release\\PrometheusStartup.exe" - all fine

Now it gets weird.
I wanted to start the App on logon. When I created a task in Windows Task Scheduler the App won't start because of an exception. When I schedule the powershell (which opens the .exe) and it starts, the App won't start because of the same exception. When I put my App through code into the registry to autostart, same exception.

Here is what eventlog says:

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 .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 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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