开发者

Administrator Privilege error

I am using following code to get a directory of files with a particular extension. It throws an error that you donot have administrator privileges. Please advise how to fix it.

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim di As New DirectoryInfo("c:\")
        Dim files() As FileInfo = di.GetFiles开发者_运维知识库("*.abc", SearchOption.AllDirectories)
    End Sub
End Class


Give your account permssions to the root directory on the C drive (I'm assuming you're using Win7, or maybe Vista, where by default only Admins have access to C:\ I think).

Though a better solution would be to use a different directory.


I'm assuming you're running on Vista or Win7.

The reason you get this error is that a regular user account does not have access to all files in on a local drive. I'm not sure of the exact folders but most of them in the %windir% folder. I suppose it is supposed to protect your computer from unauthorised access and modification.

The only way to get around it is to explicitly run your application with higher priviledges, granting it access to the files in the process.

If you are running on a Vista or Win7, you'll have to run Visual Studio as an administrator (choose that item from the context menu) in order to get the code running.
If you're running the application as standalone (not from Visual Studio), you'll as well have to run the program as Administrator.

As a precautionary measure, you could wrap your code in a Try...Catch block so your code is safe even if an exception occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜