2010-06-22 32 views
0

我的項目已更改範圍。最初,我有一個硬編碼到我的應用程序的路徑。現在,我有一個基本上允許用戶選擇要鑽入哪個驅動器的用戶界面。它以字符串對象的形式返回路徑。但是,我不確定如何將其實現到我的代碼中。不確定將對象傳遞給我的班級

下面是UI代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     FolderSelect("Please select:"); 
    } 
    private static string FolderSelect(string txtPrompt) 
    { 

     //Now, we want to use the path information to population our folder selection initial location 
     string initialCheckoutPathDir = (@"C:\"); 
     System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialCheckoutPathDir); 
     FolderBrowserDialog FolderSelect = new FolderBrowserDialog(); 
     FolderSelect.SelectedPath = info.FullName; 
     FolderSelect.Description = txtPrompt; 
     FolderSelect.ShowNewFolderButton = true; 
     if (FolderSelect.ShowDialog() == DialogResult.OK) 
     { 
      string retPath = FolderSelect.SelectedPath; 
      if (retPath == null) 
      { 
       retPath = ""; 
      } 
      return retPath; 
     } 
     else 
     { 
      return ""; 
     } 
    } 

} 

我如何採取代碼,並將其傳遞到這個代碼?

//recurse through files. Let user press 'ok' to move onto next step   
     string[] files = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories); 
     foreach (string file in files) 
     { 
      Console.Write(file + "\r\n"); 
     } 
     Console.WriteLine("Press any key to continue..."); 
     Console.ReadKey(true); 
     //End section 


     //Regex -- find invalid chars 
     string pattern = " *[\\~#%&*{}/<>?|\"-]+ *"; 
     string replacement = " "; 
     Regex regEx = new Regex(pattern); 

     string[] fileDrive = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories); 
     List<string> filePath = new List<string>(); 

     //clean out file -- remove the path name so file name only shows 

     foreach(string fileNames in fileDrive) 
     { 
     filePath.Add(fileNames); 
     } 

     using (StreamWriter sw = new StreamWriter(@"S:\bob.smith\File_Renames.txt")) 
     { 
      //Sanitize and remove invalid chars 
      foreach (string Files2 in filePath) 
      { 
       try 
       { 
        string filenameOnly = Path.GetFileName(Files2); 
        string pathOnly = Path.GetDirectoryName(Files2); 
        string sanitizedFilename = regEx.Replace(filenameOnly, replacement); 
        string sanitized = Path.Combine(pathOnly, sanitizedFilename); 
        sw.Write(sanitized + "\r\n"); 
        System.IO.File.Move(Files2, sanitized); 

       } 
       //error logging 
       catch(Exception ex) 
       { 
        StreamWriter sw2 = new StreamWriter(@"S:\bob.smith\Error_Log.txt"); 
        sw2.Write("ERROR LOG"); 
        sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n"); 
        sw2.Flush(); 
        sw2.Close(); 

       } 
      } 
     } 

    } 

此外,我怎麼能讓我的控制檯輸出只有文件有無效的字符?現在它顯示我的應用程序鑽入的所有文件。

任何幫助表示讚賞 - 這個項目對我來說有點困難,因爲我對這個東西很新!

+0

如何管理保存包含無效字符的名稱的文件?這些是由不同的操作系統保存的,你是通過Samba訪問它們的嗎? – slugster 2010-06-22 14:13:52

+0

因此,我將手動備份文件(以防在此應用程序進程中任何事件被損壞)。文件名稱本身會發生變化 - 這是爲了將項目遷移到SharePoint中。 – yeahumok 2010-06-22 15:04:24

+0

「我的項目改變了範圍,我需要創建和使用......你能相信它嗎......一個全新的VARIABLE !!!」。對不起,我是個傻子,但這讓我笑了起來 - 「我的項目改變了範圍」部分。 – 2010-06-22 16:36:09

回答

0

選擇文件夾後,您需要將該字符串保存到變量中。你可以把它叫做selectedFolder。然後,您將要用selectedFolder替換所有硬編碼目錄,@「S:\ bob.smith \」實例。

selectedFolder = FolderSelect("Please select:"); 
0

而不是在FolderSelect函數內返回returnPath,您可以將它保存到類中的變量。通過這種方式,代碼的下一部分可以訪問它,無論是因爲它在類中還是通過getter。該變量可以包含用戶指定的最後一條路徑,或者如果存在多個路徑,則可以擁有一個包含所有路徑的數組。

相關問題