-1

I am currently working on an antivirus in C#. I have one small problem though. When it scans the computer's files, it can't access files in AppData. How would I be able to search all directories except for one?

This is what I got so far(does not work):

Directory.GetFiles(path , "*.*", SearchOption.AllDirectories).Where(d => !d.StartsWith("<EXCLUDE_DIR_PATH>")).ToArray();

I keep on getting the error

Access to the path 'c:\Users\admin\AppData\Local\Application Data' is denied.

chwarr
  • 6,777
  • 1
  • 30
  • 57

2 Answers2

0

An access denied exception simply means you are not allowed to access the directory. Windows has several of those, and AppData is one of those. Try running the program as administrator. Do note that you need to

  1. be a Windows user part of the Administrators group and
  2. your app need to be run elevated.

You can achieve both 1 and 2 by running your program's executable by right clicking and selecting 'Run as administrator' (even if your logged in as an administrator!)

chwarr
  • 6,777
  • 1
  • 30
  • 57
sommmen
  • 6,570
  • 2
  • 30
  • 51
0

I have tested your code and it works fine. It seems to be a permission issue. Try running your application with Administrator permissions. Here is a good SO answer for that

However, you will run into another problem - Directory.GetFiles will find non-existent files. Another SO answer to look at for this

In my testing I ran into non-existent folders in the AppData directory. You will need to tell the program to ignore such directories. Here is another answer to get you started

ServerS
  • 452
  • 3
  • 15