2015-05-19 54 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

我很感谢您在回复中的帮助和努力。我会给这条路线一个镜头,看看它是如何发展的。 –