// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.Data; public partial class DisplayDir : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack == false) { foreach (string host in Spludlow.Config.Hosts()) { this.DropDownListHosts.Items.Add(host); if (host == Environment.MachineName) this.DropDownListHosts.SelectedIndex = this.DropDownListHosts.Items.Count - 1; } this.DropDownListHosts_SelectedIndexChanged(null, null); } } protected void DropDownListHosts_SelectedIndexChanged(object sender, EventArgs e) { string host = this.DropDownListHosts.SelectedValue; this.LiteralCurrentDirectory.Text = null; this.GridViewDrives.SelectedIndex = -1; this.GridViewDirectory.SelectedIndex = -1; this.GridViewDirectory.DataSource = null; this.GridViewDirectory.DataBind(); this.GridViewDrives.DataSource = Spludlow.Io.DirectoryList.ListDrives(host); this.GridViewDrives.DataBind(); this._ShowDirectory(); } protected void GridViewDrives_SelectedIndexChanged(object sender, EventArgs e) { string driveName = (string)this.GridViewDrives.DataKeys[this.GridViewDrives.SelectedIndex][0]; this.LiteralCurrentDirectory.Text = driveName; this.GridViewDirectory.SelectedIndex = -1; this.GridViewDirectory.DataSource = null; this.GridViewDirectory.DataBind(); this._ShowDirectory(); } protected void GridViewDirectory_SelectedIndexChanged(object sender, EventArgs e) { int index = this.GridViewDirectory.SelectedIndex; string type = (string)this.GridViewDirectory.DataKeys[index]["Type"]; string path = (string)this.GridViewDirectory.DataKeys[index]["Path"]; if (type == "D") { this.GridViewDirectory.SelectedIndex = -1; this.LiteralCurrentDirectory.Text = path; this._ShowDirectory(); } } protected void LinkButtonPathParts_Command(object sender, CommandEventArgs e) { LinkButton linkButton = (LinkButton)sender; DataListItem item = (DataListItem)linkButton.NamingContainer; this.LiteralCurrentDirectory.Text = (string)this.DataListPathParts.DataKeys[item.ItemIndex]; this.GridViewDirectory.SelectedIndex = -1; this._ShowDirectory(); } private void _ShowDirectory() { string path = this.LiteralCurrentDirectory.Text; if (path == null || path.Length == 0) { this.LiteralInfo.Text = "Nothing Selected"; this.DataListPathParts.DataSource = null; this.DataListPathParts.DataBind(); return; } string host = this.DropDownListHosts.SelectedValue; DataTable table; try { table = Spludlow.Io.DirectoryList.List(host, path, false, false).Tables[0]; } catch (Exception ee) { this.LabelError.Text = ee.Message; return; } DataView view = new DataView(table); view.RowFilter = "Type = 'D'"; int dirCount = view.Count; view.RowFilter = "Type = 'F'"; int fileCount = view.Count; if (dirCount == 0 && fileCount == 0) this.LiteralInfo.Text = "Empty Directory " + path; else this.LiteralInfo.Text = dirCount + " Directories, " + fileCount + " Files, " + path; view.RowFilter = null; view.Sort = "Type, Path"; this.GridViewDirectory.DataSource = view; this.GridViewDirectory.DataBind(); this.DataListPathParts.DataSource = this.PathParts(path); this.DataListPathParts.DataBind(); } public Boolean DirectoryBold(string type) { return (type == "D"); } public DataTable PathParts(string directory) { DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Path", typeof(string)); string[] parents = Spludlow.Text.Split(directory, '\\', true, false); StringBuilder parentPath = new StringBuilder(); for (int index = 0; index < parents.Length; ++index) { string parent = parents[index]; if (index == 0) { parentPath.Append(parent); parentPath.Append(@"\"); } else { if (index == 1) { parentPath.Append(parent); } else { parentPath.Append(@"\"); parentPath.Append(parent); } } table.Rows.Add(new object[] { parent, parentPath.ToString() }); } return table; } public string DataSize(string type, ulong length) { if (type == "D") return "-"; if (length == 0) return "0"; return Spludlow.Text.DataSize((long)length); } public void Refresh() { this._ShowDirectory(); string host = this.DropDownListHosts.SelectedValue; this.GridViewDrives.DataSource = Spludlow.Io.DirectoryList.ListDrives(host); this.GridViewDrives.DataBind(); } public string CurrentHost() { return this.DropDownListHosts.SelectedValue; } public string CurrentDirectory() { if (this.LiteralCurrentDirectory.Text.Length == 0) return null; return this.LiteralCurrentDirectory.Text; } public string SelectedFile() { int index = this.GridViewDirectory.SelectedIndex; if (index == -1) return null; if (index >= this.GridViewDirectory.DataKeys.Count) { this.GridViewDirectory.SelectedIndex = -1; return null; } string type = (string)this.GridViewDirectory.DataKeys[index]["Type"]; string path = (string)this.GridViewDirectory.DataKeys[index]["Path"]; if (type != "F") return null; return path; } public void UnSelectFiles() { this.GridViewDirectory.SelectedIndex = -1; this.Refresh(); } public string BarColour(object percentObj) { if (percentObj == null || percentObj.GetType() != typeof(decimal)) return ""; decimal percent = (decimal)percentObj; decimal red = 0xFF; if (percent < 50) red = percent * 5.1M; decimal green = 0xFF; if (percent >= 50) green = (50 - (percent - 50)) * 5.1M; return "#" + ((int)red).ToString("X2") + ((int)green).ToString("X2") + "00"; } }