如何编写可以搜索用户目录的控制台应用程序?

时间:2009-12-28 10:04:55

标签: c#

我问过大师,但我仍然无法解决我的问题。 我想编写一个控制台程序来搜索某些文件,例如 xls doc 或* pdf。 我写了这样的代码,但是当说到用户目录时,它会引发UnauthorizedAccessException。 如何编写可以搜索用户目录的控制台应用程序? 我将clickonce设置为off并使用需要管理员的清单构建它。 因此,在Vista或7上,它以管理员身份运行,具有提升对话框。

这是完整的代码。

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 & 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 > 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 > 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();
    }
}

}

1 个答案:

答案 0 :(得分:1)

您获得的错误是由文件系统权限引起的。唯一的方法是授予您正在使用的凭据访问指定文件夹,以“管理员”身份运行应用程序或以每个用户文件夹的特定用户身份运行应用程序。