|
VB .NET Helper Newsletter
|
Rod Stephens
|
May 30, 2009 06:43 PDT
|
Just a short one this week.
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB.NET Contents:
1. New HowTo: Manage the wastebasket in Visual Basic .NET
2. New HowTo: Use My.Computer.Info to display operating system and
memory information in Visual Basic .NET
Both Contents:
3. New Links
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Manage the wastebasket in Visual Basic .NET
http://www.vb-helper.com/howto_net_move_file_to_wastebasket.html
http://www.vb-helper.com/HowTo/howto_net_move_file_to_wastebasket.zip
This example uses interop methods to define functions in DLLs so it
imports the System.Runtime.InteropServices namespace.
It then declares the SHQueryRecycleBin and SHEmptyRecycleBin API
functions, and the structures they need.
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure SHQUERYRBINFO
Dim cbSize As Int32
Dim i64Size As Long
Dim i64NumItems As Long
End Structure
<DllImport("shell32.dll")> _
Private Shared Function SHQueryRecycleBin(ByVal pszRootPath As String,
ByRef ptSHQueryRBInfo As SHQUERYRBINFO) As Int32
End Function
Private Enum RecycleFlags As Int32
SHERB_NOCONFIRMATION = &H1
SHERB_NOPROGRESSUI = &H2
SHERB_NOSOUND = &H4
End Enum
<DllImport("shell32.dll")> _
Private Shared Function SHEmptyRecycleBin(ByVal hwnd As IntPtr, ByVal
pszRootPath As String, ByVal dwFlags As RecycleFlags) As Int32
End Function
Every second a timer executes the following code. It uses the
SHQueryRecycleBin function to see how many files are in the recycle bin
and how much space they occupy.
Private Sub tmrCheckBin_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrCheckBin.Tick
Static had_error As Boolean = False
If had_error Then Exit Sub
Try
Dim info As SHQUERYRBINFO
info.cbSize = Len(info)
SHQueryRecycleBin(Nothing, info)
lblNumItems.Text = info.i64NumItems & " items (" & info.i64Size
& " bytes)"
Catch ex As Exception
had_error = True
MessageBox.Show("Error getting wastebasket information" & vbCrLf
& ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
When you click the Delete File button, the following code executes. It
examines the form's radio buttons to see whether it should permanently
delete the file or move it to the wastebasket. It examines the
chkConfirmDelete check box to see if it should make the user confirm the
deletion.
The code then calls My.Computer.FileSystem.DeleteFile to delete the
file. (This part of the program is purely .NET. Too bad there aren't
similar methods for checking the wastebasket's size and emptying it.)
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim opt_recycle As FileIO.RecycleOption
If radDeletePermanently.Checked Then
opt_recycle = FileIO.RecycleOption.DeletePermanently
Else
opt_recycle = FileIO.RecycleOption.SendToRecycleBin
End If
Dim opt_confirm As FileIO.UIOption
If chkConfirmDelete.Checked Then
opt_confirm = FileIO.UIOption.AllDialogs
Else
opt_confirm = FileIO.UIOption.OnlyErrorDialogs
End If
Try
My.Computer.FileSystem.DeleteFile( _
txtFile.Text, opt_confirm, opt_recycle)
Catch ex As Exception
MessageBox.Show("Error deleting file" & vbCrLf & ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MakeDummyFile()
End Sub
When you click the Empty Wastebasket button, the following code
executes. The code sets appropriate options to optionally display a
progress bar, play the recycling sound, and ask the user for
confirmation. It then calls the SHEmptyRecycleBin API function to empty
the wastebasket.
Private Sub btnEmpty_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEmpty.Click
Dim options As RecycleFlags = 0
If Not chkProgress.Checked Then options = options Or
RecycleFlags.SHERB_NOPROGRESSUI
If Not chkPlaySound.Checked Then options = options Or
RecycleFlags.SHERB_NOSOUND
If Not chkConfirmEmpty.Checked Then options = options Or
RecycleFlags.SHERB_NOCONFIRMATION
Try
SHEmptyRecycleBin(Nothing, Nothing, options)
Catch ex As Exception
MessageBox.Show("Error emptying wastebasket" & vbCrLf &
ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Unfortunately the SHEmptyRecycleBin function seems to ignore some of
these parameters and does play the recycling sound or display a progress
dialog for me. If you figuer out how to fix this, <A
HREF="mailto:RodSte-@vb-helper.com">please let me know</A>.
==========
2. New HowTo: Use My.Computer.Info to display operating system and
memory information in Visual Basic .NET
http://www.vb-helper.com/howto_net_computer_info.html
http://www.vb-helper.com/HowTo/howto_net_computer_info.zip
When the program loads, it uses the following code to display system
information available in My.Computer.Info.
lblOSFullName.Text = My.Computer.Info.OSFullName
lblOSPlatform.Text = My.Computer.Info.OSPlatform
lblOSVersion.Text = My.Computer.Info.OSVersion
lblTotPhys.Text = FormatNumber(My.Computer.Info.TotalPhysicalMemory, 0)
& " bytes"
lblTotVirt.Text = FormatNumber(My.Computer.Info.TotalVirtualMemory, 0) &
" bytes"
lblAvailPhys.Text =
FormatNumber(My.Computer.Info.AvailablePhysicalMemory, 0) & " bytes"
lblAvailVirt.Text =
FormatNumber(My.Computer.Info.AvailableVirtualMemory, 0) & " bytes"
See also "Get day, month, date, time, and number format information for
the computer's locale in Visual Basic 2005"
(howto_2005_locale_date_info.html), which uses
My.Computer.Info.InstalledUICulture to get date and time format
information.
==========
++++++++++
<Both>
++++++++++
==========
3. New Links
http://www.vb-helper.com/links.html
CodeRush Xpress for C# and VB
http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX
A tool that adds a bunch of refactoring and software development
features to the IDE including: duplicate lines, highlighting references,
create overload, reorder parameters, and much more. Big drawback:
doesn't support Visual Studio Express Editions. (Ironic since the word
"Xpress" is in this product's name.)
Refactor! Pro for Visual Studio
http://www.devexpress.com/Products/Visual_Studio_Add-in/Refactoring
Refactoring tools for Visual Studio including: case to conditional,
extract method, For to For Each, create setter, etc. $99.
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|