2013-03-04 49 views
0

我想訪問我的目錄的路徑,但我不能。我把一個斷點在我的代碼:我如何訪問我的目錄

string directoryPath = args[0]; 

當我在args[0];點擊,這表明我這個圖片:

-  args {string[3]} string[] 
     [0] "C:\\Users\\soft\\Documents\\Visual" string 
     [1] "Studio" string 
     [2] "2010\\Projects\\erereerer\\erereerer\\bin\\Debug\\MAXee\\" string 
     directoryPath null string 
     filesList null string[] 
     filesListTmp null string[] 
     opList null erereerer.IFileOperation[] 

我一直在嘗試訪問自己的目錄,但我已經失敗。我試了很多次,但是當我運行我的代碼,而該目錄是實際上不存在的說法目錄..

這是我的代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     string directoryPath = args[0]; 
     string[] filesList, filesListTmp; 
     IFileOperation[] opList = { new FileProcNameAfter10(), 
            new FileProcEnc(), 
            new FileProcByExt("jpeg"), 
            new FileProcByExt("jpg"), 
            new FileProcByExt("doc"), 
            new FileProcByExt("pdf"), 
            new FileProcByExt("djvu") 
    }; 

    if (Directory.Exists(directoryPath)) 
    { 
     filesList = Directory.GetFiles(directoryPath); 
     while (true) 
     { 
     Thread.Sleep(500); 
     filesListTmp = Directory.GetFiles(directoryPath); 

     foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList)) 
     { 
      Console.WriteLine(elem); 

      foreach (var op in opList) 
      { 
       if (op.Accept(elem)) op.Process(elem); 
      } 
     } 
      filesList = filesListTmp; 
      if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break; 
     } 
    } 

    else 
    { 
     Console.WriteLine("There is no such directory."); 
     Console.ReadKey(); 
    } 
    } 
} 
+0

你是如何調用你的程序? – cdhowie 2013-03-04 22:12:23

+4

在雙引號之間傳遞參數 – Steve 2013-03-04 22:14:16

+0

@cdhowie即時編譯,但它返回的消息當目錄實際存在時,沒有這樣的目錄 – thanks 2013-03-04 22:16:33

回答

0

無論是通過目錄報價:

MyProgram.exe "C:\Users\soft\Documents\Visual Studio 2010\Projects\erereerer\erereerer\bin\Debug\MAXee\" 

Joinargs在代碼:

directoryPath = String.Join(" ", args); 
+0

當我通過雙引號即時通訊再次獲取此[0] =「C:\\ Users \\ soft \\ Documents \\ Visual Studio 2010 \\ Projects \\ erereerer \\ erereerer \\ bin \\ Debug \\ MAXee \「」 – thanks 2013-03-04 22:21:02

+0

但這就是你想要的,不是嗎?完整的目錄?第二個解決方案有幫助嗎? – Blachshma 2013-03-04 22:22:33

+0

但我得到一個消息,該目錄不存在,我認爲當這是照顧目錄將show – thanks 2013-03-04 22:26:21

2

[0] 「C:\用戶\軟\文件\視覺」 的字符串
[1] 「工作室」 串
[2]「2010 \項目\ erereerer \ erereerer \ BIN \調試\ MAXee \ 「string

它告訴我你正在傳遞沒有引號的參數。

叫你這樣編程:

MyApp.exe "C:\Users\soft\Documents\Visual Studio 2010\Projects\erereerer\erereerer\bin\Debug\MAXee\" 

或者只是做Blachshma說:

directoryPath = String.Join(" ", args); 
+1

我會反對加入字符串,因爲調用程序的人應該首先將路徑作爲一個參數傳遞。它最終取決於是否想要記錄此特定(錯誤)功能,並且必須在將來的版本中支持它。 – cdhowie 2013-03-04 22:20:56

+0

@cdhowie當然,我只是展示了所有可能的方法來完成它。 OP必須看到對他/她最好的東西。 – 2013-03-04 22:22:30