summaryrefslogtreecommitdiff
path: root/Noddybox.WindowsPhone.Silverlight/ViewModels/FileInfo.cs
blob: 9adb0f38c9f6f424b134bb6abb71e8e69a093b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is part of the Noddybox.WindowsPhone C# suite.
//
// Noddybox.Emulation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Noddybox.Emulation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Noddybox.Emulation.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (c) 2012 Ian Cowburn
//
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Noddybox.WindowsPhone.Silverlight.ViewModels
{
    /// <summary>
    /// Provides information on a file in Isolated Storage
    /// </summary>
    public class FileInfo
    {
        /// <summary>
        /// True if the file is a folder.
        /// </summary>
        public bool IsFolder {get; private set;}

        /// <summary>
        /// The name of the file.
        /// </summary>
        public string Name {get; private set;}

        /// <summary>
        /// The full path of the file.
        /// </summary>
        public string Path {get; private set;}

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="isFolder">Whether the file is a folder.</param>
        /// <param name="name">The file name.</param>
        public FileInfo (bool isFolder, string name, string path)
        {
            IsFolder = isFolder;
            Name = name;
            Path = path;
        }

        /// <summary>
        /// Gets the file list for a specified folder.
        /// </summary>
        /// <param name="folder">The folder to search, which must end with a "/".  Uses root folder is empty or null.</param>
        /// <returns>A list of the files and directories.  A ".." directory is added if not the root folder.</returns>
        public static List<FileInfo> GetFiles(string folder, string mask)
        {
            bool root = false;
            List<FileInfo> flist = new List<FileInfo>();

            if (String.IsNullOrEmpty(folder) || folder == "/")
            {
                root = true;
                folder = "/";
            }

            if (!folder.EndsWith("/"))
            {
                throw new ArgumentException("Folder must end with a slash");
            }

            if (!root)
            {
                flist.Add(new FileInfo(true, "..", folder + ".."));
            }

            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();

            List<string> folders = new List<string>(iso.GetDirectoryNames(folder + "*"));
            List<string> files = new List<string>(iso.GetFileNames(folder + mask));

            folders.Sort();
            files.Sort();

            foreach (string s in folders)
            {
                flist.Add(new FileInfo(true, s, folder + s));
            }

            foreach (string s in files)
            {
                flist.Add(new FileInfo(false, s, folder + s));
            }

            return flist;
        }
    }
}