Tuesday, September 13, 2011

9.13.11

Public Class Recipe05_17

Public Shared Sub Main()

Dim drive As New DriveInfo("C:\")

Console.Write("Free space in {0}-drive (in kilobytes): ", drive.Name)
Console.WriteLine(drive.AvailableFreeSpace / 1024)


End Sub
End Class


Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
Dim dlg As New OpenFileDialog

dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|All Files (*.*)|*.*"
dlg.CheckFileExists = True
dlg.InitialDirectory = Application.StartupPath

If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
rtDoc.LoadFile(dlg.FileName)
rtDoc.Enabled = True
End If
End Sub

Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
Dim dlg As New SaveFileDialog

dlg.Filter = "Rich Text Files (*.rtf)|*.RTF" & "|All Files (*.*)|*.*"
dlg.InitialDirectory = Application.StartupPath

If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
rtDoc.SaveFile(dlg.FileName)
End If
End Sub

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Me.Close()
End Sub


Imports System
Imports System.IO
Imports System.IO.IsolatedStorage

Namespace Apress.VisualBasicRecipes.Chapter05

Public Class Recipe05_19
Public Shared Sub Main()

'create the store for the current user
Using store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly
'create a folder in the root of the isolated store.
store.CreateDirectory("MyFolder")

'create a file in the isolated store.
Using fs As New IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store)
Dim w As New StreamWriter(fs)

'you can now write to the file as normal
w.WriteLine("Test")
w.Flush()
End Using

Using fs As New IsolatedStorageFileStream("OtherFile.txt", FileMode.Create, store)
Dim w As New StreamWriter(fs)

'you can now write to the file as normal
w.WriteLine("Other test")
w.Flush()
End Using

Console.WriteLine("Current size: " & store.CurrentSize.ToString)
Console.WriteLine("Scope: " & store.Scope.ToString)
Console.WriteLine("Contained files include:")

Dim files As String() = store.GetFileNames("*.*")
For Each File As String In files
Console.WriteLine(File)
Next

End Using
End Sub
End Class
End Namespace


Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Text

Namespace Apress.VisualBasicRecipes.Chapter05

Public Class Recipe05_20
Public Shared Sub Main()

Using watch As New FileSystemWatcher

watch.Path = Application.StartupPath
watch.Filter = "*.*"
watch.IncludeSubdirectories = True

'attach the event handlers
AddHandler watch.Created, AddressOf OnCreatedOrDeleted
AddHandler watch.Deleted, AddressOf OnCreatedOrDeleted
watch.EnableRaisingEvents = True

Console.WriteLine("Press Enter to create a file")
Console.ReadLine()

If File.Exists("test.bin") Then
File.Delete("test.bin")
End If

'create test.bin file
Using fs As New FileStream("test.bin", FileMode.Create)
Dim info As Byte() = New UTF8Encoding(True).GetBytes("hello world")
fs.Write(info, 0, info.Length)
End Using

End Using
End Sub

Private Shared Sub OnCreatedOrDeleted(ByVal sender As Object, ByVal e As FileSystemEventArgs)
'display the notification information
Console.WriteLine("{0}NOTIFICATION: {1} was {2}", ControlChars.Tab, e.FullPath, e.ChangeType.ToString)
Console.WriteLine()
End Sub

End Class

End Namespace

No comments: