Disable X Close Button
代码
#Region
"Disable the 'X'"
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase .CreateParams
Const CS_NOCLOSE As Integer = & H200
cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
Return cp
End Get
End Property
#End Region
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase .CreateParams
Const CS_NOCLOSE As Integer = & H200
cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
Return cp
End Get
End Property
#End Region
Disable Alt+F4
代码
[VB.NET]
Protected Overrides Sub WndProc( _
ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = & H112
Const SC_CLOSE As Integer = & HF060
If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32() = SC_CLOSE Then
Return
End If
MyBase .WndProc(m)
End Sub
Protected Overrides Sub WndProc( _
ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = & H112
Const SC_CLOSE As Integer = & HF060
If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32() = SC_CLOSE Then
Return
End If
MyBase .WndProc(m)
End Sub
隱藏任務欄
代码
#Region
"隐藏或显示任务栏"
Private Const SWP_HIDEWINDOW As Integer = & H80
Private Const SWP_SHOWWINDOW As Integer = & H40
Private Const SWP_NOSIZE As Integer = & H1
Private Const SWP_NOMOVE As Integer = & H2
Private Const SWP_NOACTIVATE As Integer = & H10
Private Const HWND_TOPMOST As Integer = - 1
Private Const HWND_NOTOPMOST As Integer = - 2
< DllImport( " user32.dll " ) > _
Private Shared Function SetWindowPos( _
ByVal hWnd As Integer , _
ByVal hWndInsertAfter As Integer , _
ByVal X As Short , _
ByVal Y As Short , _
ByVal cx As Short , _
ByVal cy As Short , _
ByVal uFlags As UInt32 _
) As Boolean
End Function
< DllImport( " user32.dll " ) > _
Private Shared Function FindWindow( _
ByVal lpClassName As String , _
ByVal lpWindowName As String _
) As Integer
End Function
Private Sub ShowOrHideTaskbar( ByVal isShow As Boolean )
Dim TaskBarHwnd As Integer = FindWindow( " Shell_traywnd " , "" )
If isShow Then
SetWindowPos(TaskBarHwnd, 0 , 0 , 0 , 0 , 0 , Convert.ToUInt32(SWP_SHOWWINDOW))
Else
' when browswer open new window,will not active taskbar
SetWindowPos(TaskBarHwnd, 0 , 0 , 0 , 0 , 0 , Convert.ToUInt32(SWP_NOACTIVATE))
' SetWindowPos(TaskBarHwnd, 0, 0, 0, 0, 0, Convert.ToUInt32(SWP_HIDEWINDOW))
End If
End Sub
#End Region
Private Const SWP_HIDEWINDOW As Integer = & H80
Private Const SWP_SHOWWINDOW As Integer = & H40
Private Const SWP_NOSIZE As Integer = & H1
Private Const SWP_NOMOVE As Integer = & H2
Private Const SWP_NOACTIVATE As Integer = & H10
Private Const HWND_TOPMOST As Integer = - 1
Private Const HWND_NOTOPMOST As Integer = - 2
< DllImport( " user32.dll " ) > _
Private Shared Function SetWindowPos( _
ByVal hWnd As Integer , _
ByVal hWndInsertAfter As Integer , _
ByVal X As Short , _
ByVal Y As Short , _
ByVal cx As Short , _
ByVal cy As Short , _
ByVal uFlags As UInt32 _
) As Boolean
End Function
< DllImport( " user32.dll " ) > _
Private Shared Function FindWindow( _
ByVal lpClassName As String , _
ByVal lpWindowName As String _
) As Integer
End Function
Private Sub ShowOrHideTaskbar( ByVal isShow As Boolean )
Dim TaskBarHwnd As Integer = FindWindow( " Shell_traywnd " , "" )
If isShow Then
SetWindowPos(TaskBarHwnd, 0 , 0 , 0 , 0 , 0 , Convert.ToUInt32(SWP_SHOWWINDOW))
Else
' when browswer open new window,will not active taskbar
SetWindowPos(TaskBarHwnd, 0 , 0 , 0 , 0 , 0 , Convert.ToUInt32(SWP_NOACTIVATE))
' SetWindowPos(TaskBarHwnd, 0, 0, 0, 0, 0, Convert.ToUInt32(SWP_HIDEWINDOW))
End If
End Sub
#End Region
得到硬盤邏輯盘的剩余空间
代码
Public
Function
GetLogicalDiskFreeSpace(
ByVal
diskName
As
String
)
As
Int64
'
得到剩余空间
Dim newDiskName As String = diskName.Substring( 0 , 1 ) + " : "
Dim disk As New ManagementObject( " win32_logicaldisk.deviceid="" " + newDiskName + " "" " )
disk.Get()
Return Convert.ToInt64(disk( " FreeSpace " ))
End Function
Dim newDiskName As String = diskName.Substring( 0 , 1 ) + " : "
Dim disk As New ManagementObject( " win32_logicaldisk.deviceid="" " + newDiskName + " "" " )
disk.Get()
Return Convert.ToInt64(disk( " FreeSpace " ))
End Function
讀寫config 文件
代码
Public Function ReadConfig( ByVal FileName As String , ByVal SectionName As String ) As String
Dim configFile As New XmlDocument
Dim xpathTemplate = " //configuration/appSettings/add[@key='{0}'] "
Dim xpath As String = String .Format(xpathTemplate, SectionName)
Dim att As String
Dim _node As XmlNode
Try
With configFile
.Load(FileName)
_node = .SelectSingleNode(xpath)
att = _node.Attributes( " value " ).InnerText
End With
Catch fnfe As FileNotFoundException
Throw fnfe
Catch xe As XmlException
Throw xe
Catch ex As Exception
End Try
configFile = Nothing
Return att
End Function
Public Sub WriteConfig( ByVal FileName As String , ByVal SectionName As String , _
ByVal SectionValue As String )
Dim configFile As New XmlDocument
Dim xpathTemplate = " //configuration/appSettings/add[@key='{0}'] "
Dim xpath As String = String .Format(xpathTemplate, SectionName)
Dim att As String
Dim _node As XmlNode
Try
With configFile
.Load(FileName)
_node = .SelectSingleNode(xpath)
' if no,insert
If _node Is Nothing Then
Dim keyattrib As XmlAttribute = configFile.CreateAttribute( " key " )
keyattrib.InnerText = SectionName
Dim valattrib As XmlAttribute = configFile.CreateAttribute( " value " )
valattrib.InnerText = SectionValue
Dim add As XmlNode = configFile.CreateElement( " add " )
add.Attributes.Append(keyattrib)
add.Attributes.Append(valattrib)
Dim appsettings As XmlNode = configFile.GetElementsByTagName( " appSettings " )( 0 )
appsettings.AppendChild(add)
Else
_node.Attributes( " value " ).InnerText = SectionValue
End If
' persist changes
.Save(FileName)
End With
Catch fnfe As FileNotFoundException
Throw fnfe
Catch xe As XmlException
Throw xe
Catch ex As Exception
Throw ex
End Try
configFile = Nothing
End Sub
Public Function ReadConfig( ByVal FileName As String , ByVal SectionName As String ) As String
Dim configFile As New XmlDocument
Dim xpathTemplate = " //configuration/appSettings/add[@key='{0}'] "
Dim xpath As String = String .Format(xpathTemplate, SectionName)
Dim att As String
Dim _node As XmlNode
Try
With configFile
.Load(FileName)
_node = .SelectSingleNode(xpath)
att = _node.Attributes( " value " ).InnerText
End With
Catch fnfe As FileNotFoundException
Throw fnfe
Catch xe As XmlException
Throw xe
Catch ex As Exception
End Try
configFile = Nothing
Return att
End Function
Public Sub WriteConfig( ByVal FileName As String , ByVal SectionName As String , _
ByVal SectionValue As String )
Dim configFile As New XmlDocument
Dim xpathTemplate = " //configuration/appSettings/add[@key='{0}'] "
Dim xpath As String = String .Format(xpathTemplate, SectionName)
Dim att As String
Dim _node As XmlNode
Try
With configFile
.Load(FileName)
_node = .SelectSingleNode(xpath)
' if no,insert
If _node Is Nothing Then
Dim keyattrib As XmlAttribute = configFile.CreateAttribute( " key " )
keyattrib.InnerText = SectionName
Dim valattrib As XmlAttribute = configFile.CreateAttribute( " value " )
valattrib.InnerText = SectionValue
Dim add As XmlNode = configFile.CreateElement( " add " )
add.Attributes.Append(keyattrib)
add.Attributes.Append(valattrib)
Dim appsettings As XmlNode = configFile.GetElementsByTagName( " appSettings " )( 0 )
appsettings.AppendChild(add)
Else
_node.Attributes( " value " ).InnerText = SectionValue
End If
' persist changes
.Save(FileName)
End With
Catch fnfe As FileNotFoundException
Throw fnfe
Catch xe As XmlException
Throw xe
Catch ex As Exception
Throw ex
End Try
configFile = Nothing
End Sub
藍牙發送文件
代码
Public
Function
BlueTooth(
ByVal
DeviceAdress
As
String
,
ByVal
FilePath
As
String
)
As
String
Try
Cursor.Current = Cursors.WaitCursor
Dim theuri As Uri
theuri = New Uri( " obex:// " + DeviceAdress + System.IO.Path.GetFileName(FilePath), True )
Dim request As New ObexWebRequest(theuri)
' request.Timeout = 1000 '1 seconds
request.ReadFile(FilePath)
Dim response As ObexWebResponse = CType (request.GetResponse(), ObexWebResponse)
Select Case response.StatusCode
Case ObexStatusCode.BadRequest
BlueTooth = " 文件发送失败 "
Case ObexStatusCode.Conflict
BlueTooth = " 发生冲突,请重试 "
Case ObexStatusCode.DatabaseFull
BlueTooth = " 空间不够,请先清理文件 "
Case ObexStatusCode.NotAcceptable
BlueTooth = " 发生未知错误 "
Case ObexStatusCode.Forbidden
BlueTooth = " 发生未知错误 "
Case ObexStatusCode.Final
BlueTooth = " 文件发送完成 "
Case 160
BlueTooth = " 文件发送完成 "
Case ObexStatusCode.GatewayTimeout
BlueTooth = " 文件发送超时 "
Case ObexStatusCode.InternalServerError
BlueTooth = " 连接中断 "
Case ObexStatusCode.NoContent
BlueTooth = " 文件内容为空 "
Case ObexStatusCode.NotFound
BlueTooth = " 找不到文件 "
Case Else
BlueTooth = response.StatusCode.ToString
End Select
response.Close()
Cursor.Current = Cursors.Default
Catch ex As Exception
BlueTooth = " 文件发送失败 "
End Try
Try
Cursor.Current = Cursors.WaitCursor
Dim theuri As Uri
theuri = New Uri( " obex:// " + DeviceAdress + System.IO.Path.GetFileName(FilePath), True )
Dim request As New ObexWebRequest(theuri)
' request.Timeout = 1000 '1 seconds
request.ReadFile(FilePath)
Dim response As ObexWebResponse = CType (request.GetResponse(), ObexWebResponse)
Select Case response.StatusCode
Case ObexStatusCode.BadRequest
BlueTooth = " 文件发送失败 "
Case ObexStatusCode.Conflict
BlueTooth = " 发生冲突,请重试 "
Case ObexStatusCode.DatabaseFull
BlueTooth = " 空间不够,请先清理文件 "
Case ObexStatusCode.NotAcceptable
BlueTooth = " 发生未知错误 "
Case ObexStatusCode.Forbidden
BlueTooth = " 发生未知错误 "
Case ObexStatusCode.Final
BlueTooth = " 文件发送完成 "
Case 160
BlueTooth = " 文件发送完成 "
Case ObexStatusCode.GatewayTimeout
BlueTooth = " 文件发送超时 "
Case ObexStatusCode.InternalServerError
BlueTooth = " 连接中断 "
Case ObexStatusCode.NoContent
BlueTooth = " 文件内容为空 "
Case ObexStatusCode.NotFound
BlueTooth = " 找不到文件 "
Case Else
BlueTooth = response.StatusCode.ToString
End Select
response.Close()
Cursor.Current = Cursors.Default
Catch ex As Exception
BlueTooth = " 文件发送失败 "
End Try
C# 泛型 对象数组排序
代码
public
void
Sort
<
T
>
(
object
[] list,
string
key,
bool
isReverse)
{
int len = list.Length;
Type type = typeof (T);
object [] keys = new object [len];
for ( int i = 0 ; i < len; i ++ )
{
// Hack,对于字符形式存储的数字,如7,8,9,10,排序,前面加0
string temp = type.InvokeMember(key, BindingFlags.GetProperty, null , list[i], null ).ToString();
if (temp.Trim().Length == 1 )
temp = " 0 " + temp.ToString();
keys[i] = temp;
}
Array.Sort(keys, list);
if (isReverse)
Array.Reverse(list);
}
{
int len = list.Length;
Type type = typeof (T);
object [] keys = new object [len];
for ( int i = 0 ; i < len; i ++ )
{
// Hack,对于字符形式存储的数字,如7,8,9,10,排序,前面加0
string temp = type.InvokeMember(key, BindingFlags.GetProperty, null , list[i], null ).ToString();
if (temp.Trim().Length == 1 )
temp = " 0 " + temp.ToString();
keys[i] = temp;
}
Array.Sort(keys, list);
if (isReverse)
Array.Reverse(list);
}