<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Hadoop.WebClient" version="0.12.5126.42915" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
<package id="System.Spatial" version="5.2.0" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="2.0.4.1" targetFramework="net45" />
</packages>
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Hadoop.WebHDFS;
namespace HdfsExplore
{
public partial class Explorer : Form
{
private WebHDFSClient client;
public Explorer()
{
InitializeComponent();
if (!CreateClient())
{
return;
}
// 使用驱动器列表填充TreeView
PopulateDriveList();
}
//此过程使用驱动器列表填充TreeView
private async void PopulateDriveList()
{
TreeNode nodeTreeNode;
var imageIndex = 0;
var selectIndex = 0;
const int Removable = 2;
const int LocalDisk = 3;
const int Network = 4;
const int CD = 5;
//const int RAMDrive = 6;
Cursor = Cursors.WaitCursor;
//清除TreeView
tvFolders.Nodes.Clear();
nodeTreeNode = new TreeNode("Hadoop Root", 0, 0);
tvFolders.Nodes.Add(nodeTreeNode);
//设置节点集合
var nodeCollection = nodeTreeNode.Nodes;
var x = await client.GetDirectoryStatus("/");
foreach (var mo in x.Directories)
{
//switch (int.Parse(mo["DriveType"].ToString()))
//{
// case Removable: //removable drives
// imageIndex = 5;
// selectIndex = 5;
// break;
// case LocalDisk: //Local drives
// imageIndex = 6;
// selectIndex = 6;
// break;
// case CD: //CD rom drives
// imageIndex = 7;
// selectIndex = 7;
// break;
// case Network: //Network drives
// imageIndex = 8;
// selectIndex = 8;
// break;
// default: //defalut to folder
// imageIndex = 2;
// selectIndex = 3;
// break;
//}
imageIndex = 6;
selectIndex = 6;
//创建新的驱动器节点
nodeTreeNode = new TreeNode(mo.PathSuffix, imageIndex, selectIndex);
//添加新节点
nodeCollection.Add(nodeTreeNode);
}
//初始化文件ListView
InitListView();
Cursor = Cursors.Default;
}
private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
{
//选择文件夹时填充文件夹和文件
Cursor = Cursors.WaitCursor;
//获取当前所选驱动器或文件夹
var nodeCurrent = e.Node;
//清除所有子文件夹
nodeCurrent.Nodes.Clear();
if (nodeCurrent.SelectedImageIndex == 0)
{
//重新填写驱动器列表
PopulateDriveList();
}
else
{
//填充子文件夹和文件夹文件
PopulateDirectory(nodeCurrent, nodeCurrent.Nodes);
}
Cursor = Cursors.Default;
}
protected void InitListView()
{
//初始化ListView控件
lvFiles.Clear();
//创建ListView的列标题
lvFiles.Columns.Add("名称", 150, HorizontalAlignment.Left);
lvFiles.Columns.Add("大小", 75, HorizontalAlignment.Right);
lvFiles.Columns.Add("上次访问", 140, HorizontalAlignment.Left);
lvFiles.Columns.Add("上次修改", 140, HorizontalAlignment.Left);
lvFiles.Columns.Add("用有者", 75);
lvFiles.Columns.Add("权限", 75);
}
protected async void PopulateDirectory(TreeNode nodeCurrent, TreeNodeCollection nodeCurrentCollection)
{
TreeNode nodeDir;
var imageIndex = 2; //未选择的图像索引
var selectIndex = 3; //选择图像索引
if (nodeCurrent.SelectedImageIndex != 0)
{
//用文件夹填充树视图
try
{
var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));
var dirs = directoryStatus.Directories;
//check path
//if (!dirs.Any())
//{
// MessageBox.Show("Directory or path " + nodeCurrent.ToString() + " does not exist.");
//}
//else
//{
//填充文件
PopulateFiles(nodeCurrent);
var stringDirectories = dirs.Select(x => x.PathSuffix).ToArray();
var stringFullPath = "";
var stringPathName = "";
//循环遍历所有目录
foreach (var stringDir in stringDirectories)
{
stringFullPath = stringDir;
stringPathName = GetPathName(stringFullPath);
//创建目录节点
nodeDir = new TreeNode(stringPathName, imageIndex, selectIndex);
nodeCurrentCollection.Add(nodeDir);
}
//}
}
catch (IOException e)
{
MessageBox.Show("Error: Drive not ready or directory does not exist.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("Error: Drive or directory access denided.");
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
}
}
protected string GetPathName(string stringPath)
{
//获取文件夹的名称
var stringSplit = stringPath.Split('\\');
var _maxIndex = stringSplit.Length;
return stringSplit[_maxIndex - 1];
}
protected async void PopulateFiles(TreeNode nodeCurrent)
{
//使用文件填充列表视图
var lvData = new string[6];
//清除列表
InitListView();
if (nodeCurrent.SelectedImageIndex != 0)
{
var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));
try
{
var files = directoryStatus.Files; // Directory.GetFiles(getFullPath(nodeCurrent.FullPath));
var stringFileName = "";
string dtCreateDate, dtModifyDate;
Int64 lFileSize = 0;
//循环遍历所有文件
foreach (var file in files)
{
stringFileName = file.PathSuffix;
//FileInfo objFileSize = new FileInfo(stringFileName);
lFileSize = file.Length;
dtCreateDate = Utility.JavaTicksToDatetime(file.AccessTime).ToString();
//GetCreationTime(stringFileName);
dtModifyDate = Utility.JavaTicksToDatetime(file.ModificationTime).ToString();
//GetLastWriteTime(stringFileName);
//创建列表视图数据
lvData[0] = GetPathName(stringFileName);
lvData[1] = formatSize(lFileSize);
lvData[2] = dtCreateDate;
lvData[3] = dtModifyDate;
lvData[4] = file.Owner;
lvData[5] = file.Permission;
//创建实际列表项
var lvItem = new ListViewItem(lvData, 0);
lvFiles.Items.Add(lvItem);
}
}
catch (IOException e)
{
MessageBox.Show("错误:驱动器未准备好或目录不存在.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("错误:驱动器或目录访问被拒绝.");
}
catch (Exception e)
{
}
// }
}
}
protected string getFullPath(string stringPath)
{
//获取完整路径
var stringParse = "";
//从路径中删除根目录.
stringParse = stringPath.Replace("Hadoop Root\\", "/").Replace("\\", "/");
return stringParse;
}
protected string getFullPath(TreeNode nodeCurrent)
{
var path = nodeCurrent.FullPath;
return getFullPath(path);
}
protected string formatSize(Int64 lSize)
{
//格式编号为KB
var stringSize = "";
var myNfi = new NumberFormatInfo();
Int64 lKBSize = 0;
if (lSize < 1024)
{
if (lSize == 0)
{
//0 byte
stringSize = "0";
}
else
{
//小于1K但不是零字节
stringSize = "1";
}
}
else
{
//转换为KB
lKBSize = lSize / 1024;
//默认格式的格式数字
stringSize = lKBSize.ToString("n", myNfi);
//移除 decimal
stringSize = stringSize.Replace(".00", "");
}
return stringSize + " KB";
}
private void menuItem2_Click(object sender, EventArgs e)
{
//退出应用
Close();
}
private void tvFolders_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
if (e.Node == null) return;
//MessageBox.Show(e.Node.FullPath);
tvFolders.SelectedNode = e.Node;
InitcmsOneRout();
cmsOneRout.Show(tvFolders, e.X, e.Y);
}
private void InitcmsOneRout()
{
cmsOneRout = new ContextMenuStrip();
var tmiEditRoutStation = new ToolStripMenuItem("修改Owner");
cmsOneRout.Items.Add(tmiEditRoutStation);
var changePermission = new ToolStripMenuItem("修改权限");
changePermission.Click += changePress_Click;
cmsOneRout.Items.Add(changePermission);
var tmiDel = new ToolStripMenuItem("删除");
tmiDel.Click += tmiDel_Click;
cmsOneRout.Items.Add(tmiDel);
}
async void changePress_Click(object sender, EventArgs e)
{
//if (lvFiles.SelectedItems.Count == 0)
// return;
if (tvFolders.SelectedNode == null)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
//var remotefile = remotepath.TrimEnd('/') + "/" + lvFiles.SelectedItems[0].Text;
var permissions = await client.GetFileStatus(remotepath);
InputName ip = new InputName("修改权限(" + remotepath + ")", permissions.Permission);
if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrWhiteSpace(ip.StrInput))
{
MessageBox.Show("不能为空项");
return;
}
await client.SetPermissions(remotepath, ip.StrInput);
}
}
private void tmiDel_Click(object sender, EventArgs e)
{
var path = getFullPath(tvFolders.SelectedNode);
//client = new WebHDFSClient(new Uri("http://zhangbaowei:50070"), "hadoop");
client.DeleteDirectory(path, true);
MessageBox.Show("删除完成");
tvFolders.SelectedNode.Remove();
}
private void menuItem5_Click(object sender, EventArgs e)
{
var parentPath = "/";
if (tvFolders.SelectedNode != null)
{
parentPath = getFullPath(tvFolders.SelectedNode);
}
var name = new InputName();
if (name.ShowDialog() != DialogResult.OK)
return;
var newname = name.StrInput;
client.CreateDirectory(parentPath.TrimEnd('/') + "/" + newname);
}
private void menuItem6_Click(object sender, EventArgs e)
{
showSetting();
PopulateDriveList();
}
private void showSetting()
{
var setting = new Setting();
if (setting.ShowDialog() == DialogResult.OK)
{
CreateClient();
}
}
private bool CreateClient()
{
var url = AppConfig.GetValue("hdfsurl");
var username = AppConfig.GetValue("username");
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(username))
{
MessageBox.Show("未进行正确配置");
showSetting();
return false;
}
client = new WebHDFSClient(new Uri(url), username);
return true;
}
private void menuItem7_Click(object sender, EventArgs e)
{
if (tvFolders.SelectedNode == null)
{
MessageBox.Show("未选中上传目标");
return;
}
OpenFileDialog of = new OpenFileDialog();
of.Multiselect = true;
if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var remotepath = getFullPath(tvFolders.SelectedNode);
var filename = of.FileNames;
foreach (var s in filename)
{
client.CreateFile(s, remotepath.TrimEnd('/') + "/" + Path.GetFileName(s));
}
PopulateFiles(tvFolders.SelectedNode);
}
}
private void menuItem8_Click(object sender, EventArgs e)
{
var items = lvFiles.SelectedItems;
var remotepath = getFullPath(tvFolders.SelectedNode);
foreach (var item in items)
{
var filename = ((ListViewItem)item).Text;
client.DeleteDirectory(remotepath.TrimEnd('/') + "/" + filename);
}
}
private async void lvFiles_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = lvFiles.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
//MessageBox.Show("The selected Item Name is: " + item.Text);
var remotepath = getFullPath(tvFolders.SelectedNode);
var remotefile = remotepath.TrimEnd('/') + "/" + item.Text;
var message = await client.OpenFile(remotefile);
var content = await message.Content.ReadAsStringAsync();
ContentViewer cv = new ContentViewer(content, remotefile);
cv.Show();
}
else
{
this.lvFiles.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
private void lvFiles_MouseClick(object sender, MouseEventArgs e)
{
//判断鼠标是否右键
if (e.Button == MouseButtons.Right)
{
if (this.lvFiles.SelectedItems.Count > 0) //选中项
{
var cmslv = new ContextMenuStrip();
var tmiEditRoutStation = new ToolStripMenuItem("权限");
tmiEditRoutStation.Click += tmiEditRoutStation_Click;
cmslv.Items.Add(tmiEditRoutStation);
var tmiDelte = new ToolStripMenuItem("删除");
tmiDelte.Click += tmiDelte_Click;
cmslv.Items.Add(tmiDelte);
var downloadFile = new ToolStripMenuItem("下载");
downloadFile.Click += downloadFile_Click;
cmslv.Items.Add(downloadFile);
cmslv.Show(lvFiles, e.Location);
//this.lvFiles.ContextMenuStrip = cms选中菜单;
}
else //未选中项
{
//this.lvFiles.ContextMenuStrip = cms未选中菜单;
}
}
}
async void downloadFile_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
var filefolder = new FolderBrowserDialog();
if (filefolder.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
var locafolder = filefolder.SelectedPath;
foreach (var lvFile in lvFiles.SelectedItems)
{
var removefilename = ((ListViewItem)lvFile).Text;
var remotefile = remotepath.TrimEnd('/') + "/" + removefilename;
var file = await client.OpenFile(remotefile);
var filebytes = await file.Content.ReadAsByteArrayAsync();
var locafile = locafolder + "/" + removefilename;
System.IO.File.WriteAllBytes(locafile, filebytes);
//lvFiles.Items.Remove((ListViewItem)lvFile);
}
MessageBox.Show("下载成功");
}
async void tmiDelte_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
foreach (var lvFile in lvFiles.SelectedItems)
{
var remotefile = remotepath.TrimEnd('/') + "/" + ((ListViewItem)lvFile).Text;
await client.DeleteDirectory(remotefile);
lvFiles.Items.Remove((ListViewItem)lvFile);
}
}
async void tmiEditRoutStation_Click(object sender, EventArgs e)
{
if (lvFiles.SelectedItems.Count == 0)
return;
var remotepath = getFullPath(tvFolders.SelectedNode);
var remotefile = remotepath.TrimEnd('/') + "/" + lvFiles.SelectedItems[0].Text;
InputName ip = new InputName("修改权限(" + remotefile + ")");
if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrWhiteSpace(ip.StrInput))
{
MessageBox.Show("不能为空项");
return;
}
await client.SetPermissions(remotefile, ip.StrInput);
}
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。