VC learning materials collection (22): How to get Windows various system catalogs

xiaoxiao2021-03-06  45

Windows various system catalogs

Www.codesky.net 2004-6-7 Free Network

Keywords:

There are a lot of articles on how to get a Windows directory with VB, but mostly speak how to get a Windows directory and system directory, sometimes we need to get a directory like "My Documents" ("My Document" path and Not fixed, can be set by ourselves, or it is possible because the system's installation path is different), how do you handle it? Let's talk about how to use VB to get this path with VB. First introduce you to two API functions, which are ShgetSpecialFolderLocation and SHGETPATHFROMIDLIST, which is weapons we used to get various paths. Function declarations: Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As LongPrivate Declare Function SHGetPathFromIDList Lib "Shell32" Alias ​​"SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As long function function and parameter description: SHGETSPECIALFOLDERLOCATION: Get a location in a special directory in the special directory list; it has three parameters, the first parameter is used to specify the owner window, usually we write "0 "Yes; the second parameter is an integer ID, which decides which directory to find, it is as follows: & H0 & 'Desktop & H2 &' Program Set & H5 & 'My Document & H6 &' Favorites & H7 & ' start & H8 & 'recently opened files & H9 &' & 'Application Data & H1B &' PrintHood & H20 & 'page temporary files & H21 &' Cookies directory & H22 & 'history the third parameter is to send & HB &' start menu & H13 & 'My Network Places & H14 &' fonts & H15 & 'ShellNew & H1A special catalog available in The address in the special directory list. SHGETPATHFROMIDLIST: Gets the exact path to this directory in the address in a special directory list. It has two parameters, the first parameter is the address in the special directory in the special directory list, that is, the address obtained by the previous function; the second parameter is a string data, used to save the returned special directory Accurate path. For example, in order to get the path of Desktop, first you need to call SHGETSPECIALFOLDERLOCATION to get the location PID in the special directory list, then call the SHGETPATHFROMIDLIST function to get the list of the list of PID points, the exact path of Desktop. Below is an example I have written to get a variety of directory paths for Windows for your reference. If you have any questions or suggestions, please feel free to give me a letter (xuhaoliang@21cn.com).

Program code is as follows: Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As LongPrivate Declare Function SHGetPathFromIDList Lib "Shell32" Alias ​​"SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String ) As LongPrivate Declare Function GetWindowsDirectory Lib "kernel32" Alias ​​"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate Declare Function GetSystemDirectory Lib "kernel32" Alias ​​"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate Declare Function GetTempPath Lib "kernel32" Alias ​​"GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As string) As LongConst MAX_LEN = 200 'string of the maximum length Const DESKTOP = & H0 &' Desktop Const pROGRAMS = & H2 & 'assembly Const MYDOCUMENTS = & H5 & 'my documents Const MYFAVORITES = & H6 &' favorites Const sTARTUP = & H7 & 'start Const rECENT = & H8 &' recently opened files Const SENDTO = & H9 & 'send Const STARTMENU = & HB &' start menu Const NETHOOD = & H13 & 'my Network Places Const FONTS = & H14 & 'Font C onst SHELLNEW = & H15 & 'ShellNewConst APPDATA = & H1A &' Application DataConst PRINTHOOD = & H1B & 'PrintHoodConst PAGETMP = & H20 &' web temporary file Const COOKIES = & H21 & 'Cookies directory Const HISTORY = & H22 &' history Private Sub Command2_Click () EndEnd Sub Private Sub Form_Load () Dim STMP AS STRING * MAX_LEN 'Stamping Results The Fixed Length String Dim Nlength As Long' Strings DIM PIDL AS Long 'The location in a special directory list'

************************************************************************************************************************************ ************ Length = getWindowsDirectory (stmp, max_len) txtwin.text = left (stmp, length) '************************** ****** Get the system directory ******************************************** Length = GetSystemDirectory (STMP, Max_len) TXTSYSTEM.TEXT = Left (stmp, length) '********************************************************************************************** ************************* Length = getTemppath (max_len, stmp) txttemp.text = left (stmp, length) '***** **************************************************************************************************************************** ******* SHGETSPECIALFOLDERLOCATION 0, Desktop, PidlshGetPathFromidList PIDL, StmptxtDesktop.text = Left (stmp, instr (stmp, chr (0)) - 1) '************* *********** Get sent to directory ************************************************************* 0, sendto, pidlshgetpathfromidlist pIDL, stmptxtsendto.text = left (stmp, instr (stmp, chr (0)) - 1) '********************** *** Get my document catalog ****************************** SHGETSPECIALFOLDERLOCATION 0, MyDocuments, PidlshgetPathFromidList PIDL, StmptXtdocument .Text = Left (stmp, instr (ST) MP, CHR (0)) - 1) '*********************** Get assembly directory ********** ************************* SHGETSPECIALLDERLOCATION 0, Programs, PidlshGetPathFromidList PIDL, StmptxtProgram.Text = Left (stmp, instr (stmp, chr (0)) - 1) '***************************************************************************************************** ******************* SHGETSPECIALFOLDERLOCATION 0, Startup, PidlshGetPathFromidList PIDL, StmptxtStart.Text = Left (stmp, INSTR (stmp, chr (0)) - 1) '** ************************************************************************************************************************ ********** SHGETSPECIALFOLDERLOCATION 0, StartMenu, PidlshgetPathFromidList PIDL, StmptxtStartMenu.Text = Left (stmp, INSTR (STMP, CHR (0)) - 1) '

************************************************************************************************************************************************************************* Special ************* SHGETSPECIALFOLDERLOCATION 0, MyFavorites, PidlshgetPathFromidList PIDL, StmptxtFavorites.text = Left (stmp, instr (stmp, chr (0)) - 1) ******* ************** Get the last open file directory ************************************* ** SHGETSPECIALFOLDERLOCATION 0, Recent, PidlShgetPathfromidlist PIDL, StmptXTRECENT.TEXT = Left (stmp, instr (stmp, chr (0)) - 1) '**************************** ****** Get online neighbor catalog ***************************************** ShgetspecialFolderLocation 0, NetHood, PidlshgetPathFromidList PIDL , stmptxtNetHood.text = left (stmp, instr (stmp, chr (0)) - 1) '************************************************************** ********************************************* SHGETSPECIALFOLDERLOCATION 0, FONTS, PIDLSHGETPATHFROMIDLIST PIDL, StmptxtFonts.Text = Left (STMP, INSTR (STMP, CHR (0)) - 1) '******************************************************************************************** ************************* SHGETSPECIALLDERLOCATION 0, cookies, pidlshgetpathfromidlist pidl, stmptxtCookies.text = left (stmp, instr (STMP, CHR (0)) - 1) '************************ Get history catalog ** ********************************* SHGETSPECIALFOLDERLOCATION 0, HISTORY, PIDLSHGETPATHFROMIDLIST PIDL, StmptXTHistory.Text = Left (stmp, INSTR) STMP, ChR (0)) - 1) '********************* Get web temporary file directory *********** ******************* SHGETSPECIALFOLDERLOCATION 0, PageTMP, PIDLSHGETPATHFROMIDLIST PIDL, STMPTXTPAGETMP.TEXT = LEFT (stmp, instr (stmp, chr (0)) - 1) '* ********************************************************************************************************************************************* ********** SHGETSPECIALFOLDERLOCATION 0, Shellnew, PidlshGetPathFromidList PIDL, StmptxtXTshellnew.text = Left (stmp, instr (stmp, chr (0)) - 1) '

转载请注明原文地址:https://www.9cbs.com/read-73793.html

New Post(0)