0

I'm trying to make an msi file for my wpf soln through InstallShield and inside this WPF soln I'm trying to access an .rtf file using this code:

 string rtfFilePath = @"Help.rtf";

using (var fs = new FileStream(rtfFilePath, FileMode.Open)){ ... }

During debug mode it works fine but when I have already created an msi file and run the msi file and install it on my machine into the path to C:/Program Files/Company_Name/Project_Name and launch the exe program file with the rtf file on the same directory/folder where the exe file is also located.

I'm getting an exception message of "Access to the path 'C:/Program Files/Company_Name/Project_Name\Help.rtf' is denied."

And an event viewer exception of:

Description: The process was terminated due to an unhandled exception. Exception Info:

System.UnauthorizedAccessException
   at System.IO.__Error.WinIOError(Int32, System.String)
   at 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)
   at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions, System.String, Boolean)
   at System.IO.FileStream..ctor(System.String, System.IO.FileMode)
  at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
   at System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
   at System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawMouseActions, Int32, Int32, Int32)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr, MS.Internal.Interop.WindowMessage, IntPtr, IntPtr, Boolean ByRef)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at Project_Name.App.Main()

What am I missing so far?

Efren
  • 1
  • 4

1 Answers1

0

Well accessing Program Files requires Admin Access so you have to run your program as administrator.

You can make your program to require administrator privilege when running, to do this please see the following question: How do I force my .NET application to run as administrator?

P.S.: Note that if possible you can avoid accessing restricted areas like Program Files and then you won't need to grant this privilege.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Ok. It works when I run the exe file as admin. But my requirement is when I run the msi file and at the end of the setup process there will be a question(through a checkbox) if the user wants to Launch the Program. Can I make it Launch the Program as admin automatically? or this post will give me the solution -> How do I force my .NET application to run as administrator? – Efren Sep 21 '18 at 00:37
  • well, the link I gave you answers that. if you force your application to run as admin, after setup when the program runs, it will run as admin (of course after prompting to the user) – Ashkan Mobayen Khiabani Sep 21 '18 at 00:39
  • If I have admin profile on the device, is there any way automatically use my profile to run the application as admin without prompt the UAC dialog. – jerry Sep 21 '18 at 02:37
  • There is a way to disable UAC in windows but it is done in windows and a single pc and not in your application – Ashkan Mobayen Khiabani Sep 21 '18 at 07:22