1

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, System.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.DispatcherFrame) 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()

2
  • 1
    does your app to need elevated right to run? Commented Jun 9, 2017 at 13:43
  • 3
    System.UnauthorizedAccessException You probably need to run as administrator Commented Jun 9, 2017 at 13:47

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@J.Krue It's because the PowerShell execution policy wasn't set and/or it wasn't being run as administrator. The code I gave you sets the execution policy (which tells windows that you trust this script to run).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.