📄 filelistbox.cs
字号:
//Filename : FileListBox.cs
//Part of : Phone Navigator C# example
//Description : Implementation of phone navigator's file list box
//Version : 3.2
//
//This example is only to be used with PC Connectivity API version 3.2.
//Compability ("as is") with future versions is not quaranteed.
//
//Copyright (c) 2007 Nokia Corporation.
//
//This material, including but not limited to documentation and any related
//computer programs, is protected by intellectual property rights of Nokia
//Corporation and/or its licensors.
//All rights are reserved. Reproducing, modifying, translating, or
//distributing any or all of this material requires the prior written consent
//of Nokia Corporation. Nokia Corporation retains the right to make changes
//to this material at any time without notice. A copyright license is hereby
//granted to download and print a copy of this material for personal use only.
//No other license to any other intellectual property rights is granted. The
//material is provided "as is" without warranty of any kind, either express or
//implied, including without limitation, any warranty of non-infringement,
//merchantability and fitness for a particular purpose. In no event shall
//Nokia Corporation be liable for any direct, indirect, special, incidental,
//or consequential loss or damages, including but not limited to, lost profits
//or revenue,loss of use, cost of substitute program, or loss of data or
//equipment arising out of the use or inability to use the material, even if
//Nokia Corporation has been advised of the likelihood of such damages occurring.
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
namespace CSFileBrowser.NET
{
//===================================================================
// FileListBox
public class FileListBox : System.Windows.Forms.ListBox
{
private string m_strCurrentPath;
//===================================================================
// Constructor
//
//===================================================================
public FileListBox()
{
m_strCurrentPath = "";
}
//===================================================================
// GetCurrentFolder
//
// Returns currently selected folder.
// If no folder is selected, returns folder, the content of which is
// currently shown.
//
//===================================================================
public string GetCurrentFolder()
{
return m_strCurrentPath;
}
//===================================================================
// GetCurrentFile
//
// Returns currently selected file.
// If no file is selected, returns empty string.
//
//===================================================================
public string GetCurrentFile()
{
string functionReturnValue = null;
functionReturnValue = "";
int index = SelectedIndex;
if (index != -1)
{
// there is a selected item
string strSelectedTxt;
strSelectedTxt = Items[index].ToString();
if (strSelectedTxt.Substring(0, 1) != "[")
{
// selected item is a file
functionReturnValue = strSelectedTxt;
}
}
return functionReturnValue;
}
//===================================================================
// PopulateList
//
// Adds folders, files and drives of current folder to listbox
//
//===================================================================
public void PopulateList(string strNewDirectory)
{
this.Items.Clear();
try {
string strPath;
if (m_strCurrentPath.Length == 0)
{
strPath = ("c:\\");
}
else if (strNewDirectory == "..") {
// Parent folder
strPath = m_strCurrentPath;
int iLastSeparator = strPath.LastIndexOf("\\");
strPath = strPath.Remove(iLastSeparator, strPath.Length - iLastSeparator);
if (strPath.Length < 3 & !strPath.EndsWith("\\"))
{
strPath = strPath + "\\";
}
}
else
{
if (m_strCurrentPath.EndsWith("\\"))
{
strPath = m_strCurrentPath + strNewDirectory;
}
else
{
strPath = m_strCurrentPath + "\\" + strNewDirectory;
}
}
DirectoryInfo dirInfo = new DirectoryInfo(strPath);
if (strPath.Length > 3) this.Items.Add("[..]");
// List directories
DirectoryInfo[] dirInfos = dirInfo.GetDirectories();
//DirectoryInfo di;
foreach (DirectoryInfo di in dirInfos) {
string dirName = "[" + di.ToString() + "]";
this.Items.Add(dirName);
}
// List files
FileInfo[] fileInfos = dirInfo.GetFiles();
//FileInfo fi;
foreach (FileInfo fi in fileInfos) {
this.Items.Add(fi.ToString());
}
// List drives if the current level is at the root
if (strPath.Length >= 0 & strPath.Length <= 3)
{
string[] strArrDrives = System.IO.Directory.GetLogicalDrives();
//string drive;
foreach (string drive in strArrDrives) {
string driveName = "[-" + drive.Substring(0, 1) + "-]";
this.Items.Add(driveName);
}
}
m_strCurrentPath = strPath;
}
catch {
// If reading directory failed offer all logical drives
string[] strArrDrives = System.IO.Directory.GetLogicalDrives();
//string drive;
foreach (string drive in strArrDrives) {
string driveName = "[-" + drive.Substring(0, 1) + "-]";
this.Items.Add(driveName);
}
}
}
//===================================================================
// OnDoubleClick
//
// User has doubleclicked in the listbox.
//
//===================================================================
protected override void OnDoubleClick(EventArgs e)
{
int iItemIdx = this.SelectedIndex;
if (iItemIdx >= 0)
{
string strItemText = this.Items[iItemIdx].ToString();
if (strItemText.Substring(0, 1) == "[")
{
if (strItemText.Substring(1, 1) == "-")
{
// drive selected
m_strCurrentPath = strItemText.Substring(2, 1) + ":\\";
PopulateList("");
}
else
{
string strNewDirectory = strItemText;
strNewDirectory = strNewDirectory.Remove(0, 1);
strNewDirectory = strNewDirectory.Remove(strNewDirectory.Length - 1, 1);
PopulateList(strNewDirectory);
}
}
}
Common.MainForm.LBL_PCFiles.Text = m_strCurrentPath;
base.OnDoubleClick(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -