c#WPF-文件访问被拒绝

时间:2018-09-21 00:25:35

标签: c# wpf filestream rtf file-access

我正在尝试通过InstallShield为我的wpf soln创建一个msi文件,并且在此WPF soln中,我正在尝试使用以下代码访问.rtf文件:

 string rtfFilePath = @"Help.rtf";

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

在调试模式下,它可以正常工作,但是当我已经创建了一个msi文件并运行该msi文件并将其安装在我的计算机上后,进入了C:/ Program Files / Company_Name / Project_Name的路径,并使用以下命令启动exe程序文件: rtf文件位于exe文件所在的目录/文件夹中。

我收到一条异常消息“拒绝访问路径'C:/ Program Files / Company_Name / Project_Name \ Help.rtf'。”

以及以下事件的事件查看器:

说明:由于未处理的异常,进程已终止。 异常信息:

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()

到目前为止我还缺少什么?

1 个答案:

答案 0 :(得分:0)

要完全访问Program Files,需要管理员访问权限,因此您必须以管理员身份运行程序。

您可以使程序在运行时要求管理员特权,为此,请参见以下问题: How do I force my .NET application to run as administrator?

P.S .:请注意,如果可以的话,您可以避免访问Program Files之类的受限区域,然后就不必授予此特权。