Enum Week
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)廣西,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
周日 = 0
周一 = 1
周二 = 2
周三 = 3
周四 = 4
周五 = 5
周六 = 6
End Enum
Sub Main()
Dim myType As Type = GetType(Week)
MsgBox(Week.GetName(myType, Week.周二))
End Sub
Option Explicit
'注冊表主鍵
Public Enum enumRegMainKey
iHKEY_CURRENT_USER = H80000001
iHKEY_LOCAL_MACHINE = H80000002
iHKEY_CLASSES_ROOT = H80000000
iHKEY_CURRENT_CONFIG = H80000005
iHKEY_USERS = H80000003
End Enum
'注冊表數(shù)據(jù)類型
Public Enum enumRegSzType
iREG_SZ = H1
iREG_EXPAND_SZ = H2
iREG_BINARY = H3
iREG_DWORD = H4
iREG_NONE = 0
iREG_DWORD_LITTLE_ENDIAN = 4
iREG_DWORD_BIG_ENDIAN = 5
iREG_LINK = 6
iREG_MULTI_SZ = 7
iREG_RESOURCE_LIST = 8
iREG_FULL_RESOURCE_DESCRIPTOR = 9
iREG_RESOURCE_REQUIREMENTS_LIST = 10
End Enum
'注冊表
Private Const ERROR_SUCCESS = 0
Private Const ERROR_BADDB = 1009
Private Const ERROR_BADKEY = 1010
Private Const ERROR_CANTOPEN = 1011
Private Const ERROR_CANTREAD = 1012
Private Const ERROR_CANTWRITE = 1013
Private Const ERROR_OUTOFMEMORY = 14
Private Const ERROR_INVALID_PARAMETER = 87
Private Const ERROR_ACCESS_DENIED = 5
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const ERROR_MORE_DATA = 234
Private Const KEY_QUERY_VALUE = H1
Private Const KEY_SET_VALUE = H2
Private Const KEY_CREATE_SUB_KEY = H4
Private Const KEY_ENUMERATE_SUB_KEYS = H8
Private Const KEY_NOTIFY = H10
Private Const KEY_CREATE_LINK = H20
Private Const SYNCHRONIZE = H100000
Private Const READ_CONTROL = H20000
Private Const WRITE_DAC = H40000
Private Const WRITE_OWNER = H80000
Private Const STANDARD_RIGHTS_REQUIRED = HF0000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Private Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Private Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Private Const KEY_EXECUTE = KEY_READ
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal lpSecurityAttributes As Long) As Long
Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwflags As Long) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Public Function GetValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
ByRef sValue As Variant, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long, lType As Long, lBuffer As Long, sBuffer As String, lData As Long
On Error GoTo GetValueErr
GetValue = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_READ, hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal 0, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
Select Case lType
Case iREG_SZ
lBuffer = 255
sBuffer = Space(lBuffer)
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal sBuffer, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
sValue = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Case iREG_EXPAND_SZ
sBuffer = Space(lBuffer)
If RegQueryValueEx(hKey, keyV, 0, lType, ByVal sBuffer, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
sValue = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Case iREG_DWORD
If RegQueryValueEx(hKey, keyV, 0, lType, lData, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
sValue = lData
Case iREG_BINARY
If RegQueryValueEx(hKey, keyV, 0, lType, lData, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
sValue = lData
End Select
If RegCloseKey(hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "獲取注冊表值時出錯"
End If
GetValue = True
Err.Clear
GetValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Description
End Function
Public Function SetValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
ByVal lType As enumRegSzType, _
ByVal sValue As Variant, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim S As Long, lBuffer As Long, hKey As Long
Dim ss As SECURITY_ATTRIBUTES
On Error GoTo SetValueErr
SetValue = False
ss.nLength = Len(ss)
ss.lpSecurityDescriptor = 0
ss.bInheritHandle = True
If RegCreateKeyEx(mainKey, subKey, 0, "", 0, KEY_WRITE, ss, hKey, S) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
Select Case lType
Case iREG_SZ
lBuffer = LenB(sValue)
If RegSetValueEx(hKey, keyV, 0, lType, ByVal sValue, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
Case iREG_EXPAND_SZ
lBuffer = LenB(sValue)
If RegSetValueEx(hKey, keyV, 0, lType, ByVal sValue, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
Case iREG_DWORD
lBuffer = 4
If RegSetValueExA(hKey, keyV, 0, lType, sValue, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
Case iREG_BINARY
lBuffer = 4
If RegSetValueExA(hKey, keyV, 0, lType, sValue, lBuffer) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
Case Else
Err.Raise vbObjectError + 1, , "不支持該參數(shù)類型"
End Select
If RegCloseKey(hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "設(shè)置注冊表時出錯"
End If
SetValue = True
Err.Clear
SetValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Description
End Function
Public Function DeleteValue(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long
On Error GoTo DeleteValueErr
DeleteValue = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_WRITE, hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
If RegDeleteValue(hKey, keyV) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
If RegCloseKey(hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
DeleteValue = True
Err.Clear
DeleteValueErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Description
End Function
Public Function DeleteKey(ByVal mainKey As enumRegMainKey, _
ByVal subKey As String, _
ByVal keyV As String, _
Optional ByRef rlngErrNum As Long, _
Optional ByRef rstrErrDescr As String) As Boolean
Dim hKey As Long
On Error GoTo DeleteKeyErr
DeleteKey = False
If RegOpenKeyEx(mainKey, subKey, 0, KEY_WRITE, hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
If RegDeleteKey(hKey, keyV) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
If RegCloseKey(hKey) ERROR_SUCCESS Then
Err.Raise vbObjectError + 1, , "刪除注冊表值時出錯"
End If
DeleteKey = True
Err.Clear
DeleteKeyErr:
rlngErrNum = Err.Number
rstrErrDescr = Err.Description
End Function
這個功能實現(xiàn)起來其實也很簡單,就是通過反射去讀取 DescriptionAttribute 的 Description 屬性的值,代碼如下所示:
/// summary
/// 返回枚舉項的描述信息。
/// /summary
/// param name="value"要獲取描述信息的枚舉項。/param
/// returns枚舉想的描述信息。/returns
public static string GetDescription(Enum value)
{
Type enumType = value.GetType();
// 獲取枚舉常數(shù)名稱。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 獲取枚舉字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 獲取描述的屬性。
DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
typeof(DescriptionAttribute), false) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
這段代碼還是很容易看懂的,這里取得枚舉常數(shù)的名稱使用的是 Enum.GetName() 而不是 ToString(),因為前者更快,而且對于不是枚舉常數(shù)的值會返回 null,不用進(jìn)行額外的反射。
當(dāng)然,這段代碼僅是一個簡單的示例,接下來會進(jìn)行更詳細(xì)的分析。
當(dāng)前文章:關(guān)于vb.net枚舉帶屬性的信息
網(wǎng)頁路徑:http://chinadenli.net/article0/hegoio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、服務(wù)器托管、用戶體驗、軟件開發(fā)、微信公眾號、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)