2015-05-19 59 views
0

我有一個類(GeneralSpecVersion)。一個屬性是「發佈」。我想從該類中取一個對象,從我的系統上已發佈的文件夾中將文件添加到該對象,然後在該文件夾中加載文件。由於可以有多個文件,該屬性返回一個字符串列表。從目錄加載附件(winforms)

我有這個「工作」,但這需要我在我的類和winform表單代碼中聲明文件路徑。我想盡量減少表單代碼並將我的邏輯保存在我的財產中。由於我習慣於使用簡單的「獲取,設置」,我甚至不確定是否正確設置了屬性代碼。

總之

從類裝載對象,添加文件名到該對象,加載文件

我類

public partial class GeneralSpecVersion : IEquatable<GeneralSpecVersion>, IComparable<GeneralSpecVersion>, IComparable 
{ 
    ... 

    private List<string> _Published; 

    public List<string> Published 
    {   
     get { 
      string path = (Data.Database.Setting.GetApplicationOption("GenSpecAttachments", "")); 
      string published = System.IO.Path.Combine(path, Acronym + "\\" + Major + "." + Minor + "\\Published"); 

      string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); 

      foreach (char c in invalid) 
      { 
       Acronym = Acronym.Replace(c.ToString(), "_"); 
      } 

      if (!Directory.Exists(published)) 
      { 
       Directory.CreateDirectory(published); 
      } 

      return _Published; 
     } 
     set { _Published = value; } 
    } 
} 

主要和次要是在其它性質班上。

代碼片段:

var genSpec = GeneralSpecification.GeneralSpecVersion.DataLoad("ELECTRICAL", "1", "01"); 
string publishedDir = "C:\\NewTest\\" 
        + dgvAcronymSearch.Rows[0].Cells[0].Value.ToString() + "\\" 
        + dgvAcronymSearch.Rows[0].Cells[2].Value.ToString() + "." 
        + dgvAcronymSearch.Rows[0].Cells[3].Value.ToString() + "\\Published"; 

foreach(string filepath in Directory.GetFiles(publishedDir)) 
{ 
    string file = Path.GetFileName(filepath); 
    genSpec.Published.Add(dgvAcronymSearch.Rows[0].Cells[0].Value.ToString()); 
    Process.Start(filepath); 
} 
+0

不知道在getter中所有其他的東西是什麼(並且在第一個'get'後它看起來非常多餘),但通常情況下,屬性看起來很好。就我個人而言,我會避免在吸氣時做太多的實際工作。如果所有這些東西都必須在每次獲得時發生,請使用一種方法。 – DonBoitnott

回答

2

有一個在代碼中的錯誤,在Acronym特殊字符都被合併後更換,必須在之前完成。

一般來說,在get屬性中創建一個目錄並不是一個好習慣。獲取應該能夠被調用而沒有副作用。

使用文件夾標籤可能是一種值得考慮的方法。例如。

private static readonly String PublishedFolderPattern = "<BaseFolder>\\<FolderName>\\<Major>.<Minor>\\Published"; 

public static String GetPublishedFolder(String FolderName, int Major, int Minor, bool CreateDirectory = false) { 
    String BaseFolder = (Data.Database.Setting.GetApplicationOption("GenSpecAttachments", "")); 
    return GetPublishedFolder(BaseFolder, FolderName, Major, Minor, CreateDirectory); 
} 

public static String GetPublishedFolder(String BaseFolder, String FolderName, int Major, int Minor, bool CreateDirectory = false) { 
    // needs to come before 
    FolderName = FolderName.Trim('\\'); 
    string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); 
    foreach (char c in invalid) 
    { 
     FolderName = FolderName.Replace(c, '_'); 
    } 

    string published = PublishedFolderPattern; 
    published = published.Replace("<BaseFolder>", BaseFolder); 
    published = published.Replace("<FolderName>", FolderName); 
    published = published.Replace("<Major>", Major.ToString()); 
    published = published.Replace("<Minor>", Minor.ToString()); 

    if (CreateDirectory && !Directory.Exists(published)) 
     Directory.CreateDirectory(published); 

    return published; 
} 
+0

我很感謝您在回覆中的幫助和努力。我會給這條路線一個鏡頭,看看它是如何發展的。 –