📄 filescout.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 1376 $</version>
// </file>
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Xml;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SharpDevelop.Gui
{
class FileIcon
{
[DllImport("shell32.dll")]
static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags);
[StructLayout(LayoutKind.Sequential)]
struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
}
enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
public static Bitmap GetBitmap(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO();
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if(bSmall) {
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
} else {
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;
}
SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
return Bitmap.FromHicon(info.hIcon);
}
}
public enum DriveType {
Unknown = 0,
NoRoot = 1,
Removeable = 2,
Fixed = 3,
Remote = 4,
Cdrom = 5,
Ramdisk = 6
}
public class DriveObject
{
string text = null;
string drive = null;
public string Drive {
get {
return drive;
}
}
class NativeMethods {
[DllImport("kernel32.dll", SetLastError=true)]
public static extern int GetVolumeInformation(string volumePath,
StringBuilder volumeNameBuffer,
int volNameBuffSize,
ref int volumeSerNr,
ref int maxComponentLength,
ref int fileSystemFlags,
StringBuilder fileSystemNameBuffer,
int fileSysBuffSize);
[DllImport("kernel32.dll")]
public static extern DriveType GetDriveType(string driveName);
}
public static string VolumeLabel(string volumePath)
{
try {
StringBuilder volumeName = new StringBuilder(128);
int dummyInt = 0;
NativeMethods.GetVolumeInformation(volumePath,
volumeName,
128,
ref dummyInt,
ref dummyInt,
ref dummyInt,
null,
0);
return volumeName.ToString();
} catch (Exception) {
return String.Empty;
}
}
public static DriveType GetDriveType(string driveName)
{
return NativeMethods.GetDriveType(driveName);
}
public static Image GetImageForFile(string fileName)
{
return IconService.GetBitmap(IconService.GetImageForFile(fileName));
}
public DriveObject(string drive)
{
this.drive = drive;
text = drive.Substring(0, 2);
switch(GetDriveType(drive)) {
case DriveType.Removeable:
text += " (Removeable)";
break;
case DriveType.Fixed:
text += " (Fixed)";
break;
case DriveType.Cdrom:
text += " (CD)";
break;
case DriveType.Remote:
text += " (Remote)";
break;
}
}
public override string ToString()
{
return text;
}
}
class IconManager
{
private static ImageList icons = new ImageList();
private static Hashtable iconIndecies = new Hashtable();
static IconManager()
{
icons.ColorDepth = ColorDepth.Depth32Bit;
}
public static ImageList List
{
get {
return icons;
}
}
public static int GetIndexForFile(string file)
{
string key;
// icon files and exe files can have their custom icons
if(Path.GetExtension(file).Equals(".ico", StringComparison.OrdinalIgnoreCase) ||
Path.GetExtension(file).Equals(".exe", StringComparison.OrdinalIgnoreCase)) {
key = file;
} else {
key = Path.GetExtension(file).ToLower();
}
// clear the icon cache
if(icons.Images.Count > 100) {
icons.Images.Clear();
iconIndecies.Clear();
}
if(iconIndecies.Contains(key)) {
return (int)iconIndecies[key];
} else {
icons.Images.Add(DriveObject.GetImageForFile(file));
int index = icons.Images.Count - 1;
iconIndecies.Add(key, index);
return index;
}
}
}
public class FileList : ListView
{
private FileSystemWatcher watcher;
// private MagicMenus.PopupMenu menu = null;
public FileList()
{
ResourceManager resources = new ResourceManager("ProjectComponentResources", this.GetType().Module.Assembly);
Columns.Add(ResourceService.GetString("CompilerResultView.FileText"), 100, HorizontalAlignment.Left);
Columns.Add(ResourceService.GetString("MainWindow.Windows.FileScout.Size"), -2, HorizontalAlignment.Right);
Columns.Add(ResourceService.GetString("MainWindow.Windows.FileScout.LastModified"), -2, HorizontalAlignment.Left);
// menu = new MagicMenus.PopupMenu();
// menu.MenuCommands.Add(new MagicMenus.MenuCommand("Delete file", new EventHandler(deleteFiles)));
// menu.MenuCommands.Add(new MagicMenus.MenuCommand("Rename", new EventHandler(renameFile)));
try {
watcher = new FileSystemWatcher();
} catch {}
if(watcher != null) {
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.EnableRaisingEvents = false;
watcher.Renamed += new RenamedEventHandler(fileRenamed);
watcher.Deleted += new FileSystemEventHandler(fileDeleted);
watcher.Created += new FileSystemEventHandler(fileCreated);
watcher.Changed += new FileSystemEventHandler(fileChanged);
}
HideSelection = false;
GridLines = true;
LabelEdit = true;
SmallImageList = IconManager.List;
HeaderStyle = ColumnHeaderStyle.Nonclickable;
View = View.Details;
Alignment = ListViewAlignment.Left;
}
void fileDeleted(object sender, FileSystemEventArgs e)
{
MethodInvoker method = delegate {
foreach(FileListItem fileItem in Items)
{
if(fileItem.FullName.Equals(e.FullPath, StringComparison.OrdinalIgnoreCase)) {
Items.Remove(fileItem);
break;
}
}
};
WorkbenchSingleton.SafeThreadAsyncCall(method);
}
void fileChanged(object sender, FileSystemEventArgs e)
{
MethodInvoker method = delegate {
foreach(FileListItem fileItem in Items)
{
if(fileItem.FullName.Equals(e.FullPath, StringComparison.OrdinalIgnoreCase)) {
FileInfo info = new FileInfo(e.FullPath);
try {
fileItem.SubItems[1].Text = Math.Round((double)info.Length / 1024).ToString() + " KB";
fileItem.SubItems[2].Text = info.LastWriteTime.ToString();
} catch (IOException) {
// ignore IO errors
}
break;
}
}
};
WorkbenchSingleton.SafeThreadAsyncCall(method);
}
void fileCreated(object sender, FileSystemEventArgs e)
{
MethodInvoker method = delegate {
FileInfo info = new FileInfo(e.FullPath);
ListViewItem fileItem = Items.Add(new FileListItem(e.FullPath));
try {
fileItem.SubItems.Add(Math.Round((double)info.Length / 1024).ToString() + " KB");
fileItem.SubItems.Add(info.LastWriteTime.ToString());
} catch (IOException) {
// ignore IO errors
}
};
WorkbenchSingleton.SafeThreadAsyncCall(method);
}
void fileRenamed(object sender, RenamedEventArgs e)
{
MethodInvoker method = delegate {
foreach(FileListItem fileItem in Items)
{
if(fileItem.FullName.Equals(e.OldFullPath, StringComparison.OrdinalIgnoreCase)) {
fileItem.FullName = e.FullPath;
fileItem.Text = e.Name;
break;
}
}
};
WorkbenchSingleton.SafeThreadAsyncCall(method);
}
void renameFile(object sender, EventArgs e)
{
if(SelectedItems.Count == 1) {
SelectedItems[0].BeginEdit();
}
}
void deleteFiles(object sender, EventArgs e)
{
if (MessageService.AskQuestion("Are you sure ?", "Delete files")) {
foreach(FileListItem fileItem in SelectedItems)
{
try {
File.Delete(fileItem.FullName);
} catch(Exception ex) {
MessageService.ShowError(ex, "Couldn't delete file '" + Path.GetFileName(fileItem.FullName) + "'");
break;
}
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
ListViewItem itemUnderMouse = GetItemAt(PointToScreen(new Point(e.X, e.Y)).X, PointToScreen(new Point(e.X, e.Y)).Y);
if(e.Button == MouseButtons.Right && this.SelectedItems.Count > 0) {
// menu.TrackPopup(PointToScreen(new Point(e.X, e.Y)));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -