How can I write a console application which can search Users Directory?
<p>I asked guru but I still couldn't solve the problem that I have.
I want to write a console program searching certain files, like <em>xls</em>, <em>doc</em> or *pdf.
I wrote a code like this but when it comes to the say, Users Directory, it cates UnauthorizedAccessException.
How can I write a console application which can search Users Directory?
I set clickonce off and build it with manifest which requireAdministrator.
So, on Vista or 7, it runs as an administrator, with the elevation dialogue.</p>

<p>Here's the full code.</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        //
    private const string FILE_NAME = "search.txt";
    private const string SEARCH_WORDS1 = "*.doc";
    private const string SEARCH_WORDS2 = "*.ppt";
    private const string SEARCH_WORDS3 = "*.jtd";
    private const string SEARCH_WORDS4 = "*.pdf";

    private const string END_WORDS = "\r\nSearch is finished.\r\n";

    //This funcion echoes the messages.
    void FileCheck()
    {
        string echo_words = "\r\nNow starts searching these files!" + SEARCH_WORDS1 + " "
                                + SEARCH_WORDS2 + " " + SEARCH_WORDS3 + " " + SEARCH_WORDS4 + " "
                                + "!\r\n";
        if (File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} is already exists. Replace it to the new one.", FILE_NAME);
            Console.WriteLine(echo_words);
            File.Delete(FILE_NAME);
            using (StreamWriter sw = File.CreateText(FILE_NAME))
            {
                sw.WriteLine(FILE_NAME + " is already exists. Replace it to the new one.\r\n");
                sw.WriteLine(echo_words);
                sw.Close();
            }
        }
        else
        {

            using (StreamWriter sw = File.CreateText(FILE_NAME))
            {
                Console.WriteLine(echo_words);
                sw.WriteLine(echo_words);
                sw.Close();
            }
        }
    }
    //This function write to a file that search is finished.
    void EndMessage()
    {
        using (StreamWriter sw = File.AppendText(FILE_NAME))
        {
            Console.WriteLine(END_WORDS);
            sw.WriteLine(END_WORDS);
            sw.Close();
        }
    }
    //This function searches files given and write to a file.
    void DirSearch(string sDir, string SEARCH_WORDS, int row)
    {
        int i;
        i = 0;
        string DeviceError = "off";

        try
        {
            foreach (var d in Directory.GetDirectories(sDir))
            {
                DirectoryInfo di = new DirectoryInfo(d);
                if ((di.Attributes &amp; FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) {
                    //ReparsePoint could not be serached
                    continue;
                }
                try
                {
                    foreach (string file in Directory.GetFiles(d, SEARCH_WORDS, SearchOption.AllDirectories))
                    {
                        Console.WriteLine(file);
                        using (StreamWriter sw = File.AppendText(FILE_NAME))
                        {
                            sw.WriteLine(file);
                            sw.Close();
                            i++;
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    //Unauthorized
                    Console.WriteLine(d + " is not allowd to be read !!");
                    using (StreamWriter sw = File.AppendText(FILE_NAME))
                    {
                        sw.WriteLine(d + " is not allowd to be read");
                        sw.Close();
                    }
                }
            }
        }
        catch (IOException)
        {
            //Device is not ready
            DeviceError = "on";
        }
        if (DeviceError == "off")
        {
            if (i &gt; 0)
            {
                Console.WriteLine(i + "numbers " + SEARCH_WORDS + " Files were found!\r\n");
                using (StreamWriter sw = File.AppendText(FILE_NAME))
                {
                    sw.WriteLine(i + "numbers " + SEARCH_WORDS + " Files were found!\r\n");
                    sw.Close();
                }
            }
            else
            {
                Console.WriteLine(SEARCH_WORDS + " Files were not found !\r\n");
                using (StreamWriter sw = File.AppendText(FILE_NAME))
                {
                    sw.WriteLine(SEARCH_WORDS + " Files were not found !\r\n");
                    sw.Close();
                }
            }
        }
    }

    //Main
    static void Main(string[] args)
    {
        Program x = new Program();
        string[] drives = Environment.GetLogicalDrives();
        int row = drives.GetLength(0);
        string my_documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        Console.WriteLine("Logical Drives are " + row + ".");
            using (StreamWriter sw = File.AppendText(FILE_NAME))
            { 
                sw.WriteLine("Logical Drives are " + row + ".");
                sw.Close();
            }
            int i = 0;
            x.FileCheck();
            while (row &gt; 0)
            {
                x.DirSearch(drives[i], SEARCH_WORDS1, row);
                x.DirSearch(drives[i], SEARCH_WORDS2, row);
                x.DirSearch(drives[i], SEARCH_WORDS3, row);
                x.DirSearch(drives[i], SEARCH_WORDS4, row);

                row--;
                i++;
            }
            x.EndMessage();
    }
}
</code></pre>

<p>}</p>
