Josh M
Josh M

Reputation: 173

Loading attachments from directory (winforms)

I have a class (GeneralSpecVersion). One property is "published". I want to take an object from that class, add files to that object from the published folder on my system, and load the files in that folder. Since there can be multiple files, the property returns a list of strings.

I have this "working", but this requires me to state the filepaths in both my class AND winform form code. I want to minimize the form code and keep my logic in my property. Since I'm just used to using a simple "get,set", I'm not even sure if I'm setting the property code correctly.

In short

Loading an object from the class, add filenames to that object, load the files

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

Major and minor are other properties in the class.

Code snippet:

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

Upvotes: 0

Views: 153

Answers (1)

Loathing
Loathing

Reputation: 5266

There is a bug in the code, special chars in Acronym are replaced after it is combined, must be done before.

In general, creating a directory in a get property is not good practice. Get should be able to be called without side-effects.

Using folder tags might be an approach that is worth considering. E.g.

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

Upvotes: 2

Related Questions