Share via


VB.net program ends due to access denied

Question

Sunday, November 22, 2020 11:03 PM

Hi.

Today I use manual routines for backing up data from my Pc to an external disk. I have tried to make a VB program (Visual Studio 2019) to do the job for me. It works except for one thing, to find and copy outlook files (.ost).

In the manual routine I use file explorer to find those objects. But the program isn’t allowed to.

The method to look for those files is to search all folders in C:\ but that ends with an error.

Exception unhandled /System. Unauthorized Access Exception. Access denied to C:\Users\All Users\Application Data

How do I get around that stop?  

Best regards

Janne

(an ordinary Swedish MS user)

All replies (6)

Monday, November 23, 2020 7:30 AM ✅Answered | 1 vote

The method to look for those files is to search all folders in C:\ but that ends with an error.

Exception unhandled /System. Unauthorized Access Exception. Access denied to C:\Users\All Users\Application Data

Did you try running your program via "Run as administrator"?

If running your program from within Visual Studio, try starting VS using
that option first.

(Usually found via the mouse right-click context menu from
Windows Explorer, or a desktop icon, or equivalent.)

  • Wayne

Monday, November 23, 2020 12:19 AM

The program is running under the context of the user account logged into the Windows O/S. If the user account doesn't have the needed permission on a resource, then it's 'access denied' for the program running under the user account. You have to find out if the user account has permission on the resource. If the user account doesn't  have the permissions, then you have to give the user account the needed permissions by using the Windows O/S manually and give the user-account the needed access and permission on the resource. And then the program when have the needed permission to access the resource when it is run.


Monday, November 23, 2020 8:14 AM

Thank you for answering. I just did so, but nothing was different.

Is there a way in VB to select the same way it is done in file explorer when you search for “.ost” on C:\ 

Janne

****************
System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Åtkomst till sökvägen C:\Users\All Users\Application Data nekas.
  Source=mscorlib
  StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at MyMonthBackup.Form1.GetFilesInFolder(String sDir) in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 143
   at MyMonthBackup.Form1.getAllFolders(String directory) in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 125
   at MyMonthBackup.Form1.getAllFolders(String directory) in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 133
   at MyMonthBackup.Form1.GetMailBoxes() in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 109
   at MyMonthBackup.Form1.CopyMail1(String wPath) in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 82
   at MyMonthBackup.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\janne\source\repos\MyMonthBackup\Form1.vb:line 29
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at MyMonthBackup.My.MyApplication.Main(String[] Args) in :line 81

  This exception was originally thrown at this call stack:
    System.IO.__Error.WinIOError(int, string)
    System.IO.FileSystemEnumerableIterator<TSource>.CommonInit()
    System.IO.FileSystemEnumerableIterator<TSource>.FileSystemEnumerableIterator(string, string, string, System.IO.SearchOption, System.IO.SearchResultHandler<TSource>, bool)
    System.IO.Directory.GetFiles(string)
    MyMonthBackup.Form1.GetFilesInFolder(String) in Form1.vb
    MyMonthBackup.Form1.getAllFolders(String) in Form1.vb
    MyMonthBackup.Form1.getAllFolders(String) in Form1.vb
    MyMonthBackup.Form1.GetMailBoxes() in Form1.vb
    MyMonthBackup.Form1.CopyMail1(String) in Form1.vb
    MyMonthBackup.Form1.Button1_Click(Object, System.EventArgs) in Form1.vb
    ...
    [Call Stack Truncated]


Monday, November 23, 2020 9:37 AM | 1 vote

Is there a way in VB to select the same way it is done in file explorer when you search for “.ost” on C:\ 

With ISearchFolderItemFactory


Monday, November 23, 2020 1:00 PM

This is an easy fix if one knows how to administer the O/S. It's not a programming problem. You could login with super user account that is the root administrator  account that has all admin power beyond your admin account. You can use Bing or Google and find out  how  to enable the super admin account  on the O/S and use it, because it has all the power as administrator that your  user admin account doesn't have.


Monday, November 23, 2020 2:25 PM

I have found way to at least handle the situation for now. Using file explorer, I could at last find out where the files (.ost) could be found.

”C:\Users\janne\AppData\Local\Microsoft\Outlook” is that folder on my unit.

To access the file without using the search path is not a problem and has never been. So instead of searching all folders I can go the specific one directly and copy from there. Not the most flexible solution but it works.

I have learnt that C:\Users\All Users\Application Data is not a directory but a search way.

Problem solved

Thank you all for trying to help me.

Janne