2009-12-28 116 views
1

我问大师,但我仍然无法解决我的问题。 我想写一个控制台程序搜索某些文件,如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(); 
    } 
} 

}

+0

将代码示例缩进额外的四个空格。目前阅读非常困难。 – 2009-12-28 10:08:34

+0

马塞洛你在开玩笑吧? – baldy 2009-12-28 15:40:37

回答

1

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

相关问题