📄 folders.cs
字号:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace PInvokeLibrary
{
/// <summary>
/// Summary description for Folders.
/// </summary>
public class Folders
{
const int MAX_PATH = 260;
/// <summary>
/// File system directory that contains file objects in the user's Recycle Bin.
/// </summary>
const int CSIDL_BITBUCKET = 0x000a;
/// <summary>
/// Virtual folder containing icons for Control Panel applications.
/// </summary>
const int CSIDL_CONTROLS = 0x0003;
/// <summary>
/// Windows CE Desktop, which is a virtual folder at the root of the
/// namespace.
/// \My Documents
/// </summary>
const int CSIDL_DESKTOP = 0x0000;
/// <summary>
/// File system directory used to physically store file objects on
/// the desktop, which is not to be confused with the desktop folder itself.
/// </summary>
const int CSIDL_DESKTOPDIRECTORY = 0x0010;
/// <summary>
/// My Computer, which is a virtual folder that contains everything
/// on the local computer: storage devices, printers, and
/// Control Panel.
/// </summary>
const int CSIDL_DRIVES = 0x0011;
/// <summary>
/// File system directory that serves as a common repository for
/// the user's favorite items.
/// \Windows\Favorites
/// </summary>
const int CSIDL_FAVORITES = 0x0016;
/// <summary>
/// Virtual folder containing fonts.
/// \Windows\Fonts
/// </summary>
const int CSIDL_FONTS = 0x0014;
/// <summary>
/// File system directory containing objects that appear in the
/// network neighborhood.
/// </summary>
const int CSIDL_NETHOOD = 0x0013;
/// <summary>
/// Network Neighborhood Folder, which is a virtual folder that
/// represents the top level of the network hierarchy.
/// </summary>
const int CSIDL_NETWORK = 0x0012;
/// <summary>
/// File system directory that serves as a common repository for documents.
/// "\My Documents"
/// </summary>
const int CSIDL_PERSONAL = 0x0005;
/// <summary>
/// Virtual folder containing installed printers.
/// </summary>
const int CSIDL_PRINTERS = 0x0004;
/// <summary>
/// File system directory that contains the user's program groups,
/// which are also file system directories.
/// \Windows\Start Menu\Programs
/// </summary>
const int CSIDL_PROGRAMS = 0x0002;
/// <summary>
/// File system directory that contains the user's most recently
/// used documents.
/// </summary>
const int CSIDL_RECENT = 0x0008;
/// <summary>
/// File system directory that contains Send To menu items.
/// </summary>
const int CSIDL_SENDTO = 0x0009;
/// <summary>
/// File system directory containing Start menu items.
/// \Windows\Start Menu
/// </summary>
const int CSIDL_STARTMENU = 0x000b;
/// <summary>
/// File system directory that corresponds to the user's Startup
/// program group.
/// \Windows\StartUp
/// </summary>
const int CSIDL_STARTUP = 0x0007;
/// <summary>
/// File system directory that serves as a common repository
/// for document templates.
/// </summary>
const int CSIDL_TEMPLATES = 0x0015;
/// <summary>
/// File system directory that serves as a common repository for
/// the user's favorite items.
/// \Windows\Favorites
/// </summary>
const int CSIDL_FAVORITES_GRYPHON = 0x0006;
/// <summary>
/// This function retrieves the path of a special folder, identified
/// by its CSIDL.
/// </summary>
/// <param name="hwndOwner">Handle to the owner window the client should
/// specify if it displays a dialog box or message box.</param>
/// <param name="lpszPath">Address of a character buffer that receives
/// the drive and path of the specified folder. This buffer must be at
/// least MAX_PATH characters in size.</param>
/// <param name="nFolder"> CSIDL that identifies the folder of interest.
/// If a virtual folder is specified, this function fails.</param>
/// <param name="fCreate">Indicates if the folder should be created if
/// it does not already exist. If this value is nonzero, the folder will
/// be created. If this value is zero, the folder will not be
/// created.</param>
/// <returns>Returns FALSE even if successful.</returns>
[DllImport("Coredll.dll")]
static extern int SHGetSpecialFolderPath
(
IntPtr hwndOwner,
StringBuilder lpszPath,
int nFolder,
int fCreate
);
/// <summary>
/// This function obtains information about the amount of space
/// available on a disk volume: the total amount of space, the total
/// amount of free space, and the total amount of free space available
/// to the user associated with the calling thread.
/// </summary>
/// <param name="directory">[in] Pointer to a null-terminated string that
/// specifies a directory on the disk of interest. This string can be a
/// UNC name.
/// If lpDirectoryName is NULL, the GetDiskFreeSpaceEx function obtains
/// information about the object store.
/// Note that lpDirectoryName does not have to specify the root directory
/// on a disk. The function accepts any directory on the disk.</param>
/// <param name="lpFreeBytesAvailableToCaller">[out] Pointer to a variable
/// to receive the total number of free bytes on the disk that are
/// available to the user associated with the calling thread.</param>
/// <param name="lpTotalNumberOfBytes">[out] Pointer to a variable to
/// receive the total number of bytes on the disk that are available
/// to the user associated with the calling thread.</param>
/// <param name="lpTotalNumberOfFreeBytes">[out] Pointer to a variable
/// to receive the total number of free bytes on the disk.
/// This parameter can be NULL.</param>
/// <returns>Nonzero indicates success. Zero indicates failure. To get
/// extended error information, call GetLastError.</returns>
[DllImport("coredll.dll")]
public static extern bool GetDiskFreeSpaceEx
(
string directory,
ref UInt64 lpFreeBytesAvailableToCaller,
ref UInt64 lpTotalNumberOfBytes,
ref UInt64 lpTotalNumberOfFreeBytes
);
/// <summary>
/// Run a test of the Folders class.
/// </summary>
/// <param name="showLine">Delegate called to show debug information</param>
public static void TestProc(MainTest.DisplayLineDelegate showLine)
{
Form f = new Form();
f.Capture = true;
IntPtr hwnd = Windows.GetCapture();
f.Capture = false;
showLine("Checking for storage cards...");
bool bFoundOne = false;
DirectoryInfo rootDir = new DirectoryInfo("\\");
foreach (FileSystemInfo fsi in rootDir.GetFileSystemInfos())
{
if ((FileAttributes.Directory | FileAttributes.Temporary) == (fsi.Attributes & (FileAttributes.Directory | FileAttributes.Temporary)))
{
showLine(fsi.Name);
bFoundOne = true;
}
}
if (!bFoundOne)
showLine("None found");
showLine("Finding Programs folder path");
StringBuilder folderName = new StringBuilder(MAX_PATH);
SHGetSpecialFolderPath(hwnd, folderName, CSIDL_PROGRAMS, 0);
if (string.Compare(folderName.ToString(), "") != 0)
{
showLine(folderName.ToString());
showLine("Checking free space in directory");
UInt64 freeBytesAvailableToCaller = 0;
UInt64 totalNumberOfBytes = 0;
UInt64 totalNumberOfFreeBytes = 0;
GetDiskFreeSpaceEx(folderName.ToString(), ref freeBytesAvailableToCaller,
ref totalNumberOfBytes, ref totalNumberOfFreeBytes);
showLine(string.Format("Available: {0} bytes", freeBytesAvailableToCaller));
showLine(string.Format("Total: {0} bytes", totalNumberOfBytes));
showLine(string.Format("Free: {0} bytes", totalNumberOfFreeBytes));
}
else
{
showLine("FAILURE: Not found");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -