从目录加载附件(winforms)

Loading attachments from directory (winforms)

我有一个 class (GeneralSpecVersion)。一个 属性 是 "published"。我想从那个 class 中获取一个对象,从我系统上发布的文件夹中将文件添加到该对象,然后将文件加载到该文件夹​​中。由于可以有多个文件,属性 returns 一个字符串列表。

我有这个 "working",但这需要我在我的 class 和 winform 表单代码中声明文件路径。我想最小化表单代码并将我的逻辑保留在我的 属性 中。由于我只是习惯使用简单的 "get,set",所以我什至不确定我是否正确设置了 属性 代码。

简而言之

正在从 class 加载一个对象,向该对象添加文件名,加载文件

我的Class

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; }
    }
}

主要和次要是 class 中的其他属性。

代码片段:

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);
}

代码中有错误,Acronym中的特殊字符在组合后被替换,必须在之前完成。

一般来说,在 get 属性 中创建目录不是好的做法。应该可以无副作用地调用 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;
}