Public Class Form1
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal uSize As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nMAX_PATH As Integer = 260
Dim sSystemDir As String = Strings.StrDup(nMAX_PATH, " ")
Dim nStrLen As Integer = GetSystemDirectory(sSystemDir, nMAX_PATH)
sSystemDir = Strings.Left(sSystemDir, nStrLen) '去除實際字串尾端由chr(0)起所截斷的部份
MsgBox(sSystemDir & "---")
End Sub
End Class
VC++2008:
#include <windows.h>
int main(int argc, TCHAR argv[])
{
//檔案處理
HANDLE hFile;
DWORD dwWritten;
//字串變數,用於儲存系統目錄
TCHAR szSystemDir[MAX_PATH];
//獲取系統目錄
GetSystemDirectory(szSystemDir, MAX_PATH);
//建立檔案systemroot.txt
hFile=CreateFile("systemroot.txt",
GENERIC_WRITE,
0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
//判斷檔案是否建立成功
if(hFile != INVALID_HANDLE_VALUE)
{
//將系統目錄系統資訊寫入檔案
if(!WriteFile(hFile,szSystemDir,lstrlen(szSystemDir),&dwWritten,NULL))
{
return GetLastError();
}
}
//關閉檔案,回傳
CloseHandle(hFile);
return 0;
}
WinBase.h:
WINBASEAPI
UINT
WINAPI
GetSystemDirectoryA(
__out_ecount_part_opt(uSize, return + 1) LPSTR lpBuffer,
__in UINT uSize
);
WINBASEAPI
UINT
WINAPI
GetSystemDirectoryW(
__out_ecount_part_opt(uSize, return + 1) LPWSTR lpBuffer,
__in UINT uSize
);
#ifdef UNICODE
#define GetSystemDirectory GetSystemDirectoryW
#else
#define GetSystemDirectory GetSystemDirectoryA
#endif // !UNICODE
MSDN:
GetSystemDirectory Function
Retrieves the path of the system directory. The system directory contains system files such as dynamic-link libraries, drivers, and font files.
This function is provided primarily for compatibility. Applications should store code in the Program Files folder and persistent data in the Application Data folder in the user's profile. For more information, see ShGetFolderPath.
UINT WINAPI GetSystemDirectory(
__out LPTSTR lpBuffer,
__in UINT uSize
);
Parameters
lpBuffer
A pointer to the buffer to receive the path. This path does not end with a backslash unless the system directory is the root directory. For example, if the system directory is named Windows\System on drive C, the path of the system directory retrieved by this function is C:\Windows\System.
uSize
The maximum size of the buffer, in TCHARs.
Return Value
If the function succeeds, the return value is the length, in TCHARs, of the string copied to the buffer, not including the terminating null character. If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path, including the terminating null character.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Applications should not create files in the system directory. If the user is running a shared version of the operating system, the application does not have write access to the system directory.
Example Code
For an example, see Getting System Information.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header
Declared in Winbase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL
Requires Kernel32.dll.
Unicode
Implemented as GetSystemDirectoryW (Unicode) and GetSystemDirectoryA (ANSI).
沒有留言:
張貼留言