📄 form1.vb
字号:
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 设计工具产生的程序代码 "
Public Sub New()
MyBase.New()
'此呼叫为 Windows Form 设计工具的必要项。
InitializeComponent()
'在 InitializeComponent() 呼叫之后加入所有的初始设定
End Sub
'Form 覆写 Dispose 以清除组件清单。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'为 Windows Form 设计工具的必要项
Private components As System.ComponentModel.IContainer
'注意: 以下为 Windows Form 设计工具所需的程序
'您可以使用 Windows Form 设计工具进行修改。
'请勿使用程序代码编辑器来修改这些程序。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.StatusBar1 = New System.Windows.Forms.StatusBar()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(112, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "取得暂存盘路径"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(128, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(296, 22)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(8, 56)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(112, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "取得临时文件档名"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(8, 96)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(112, 24)
Me.Button3.TabIndex = 3
Me.Button3.Text = "写读临时文件"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(8, 128)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(112, 24)
Me.Button4.TabIndex = 4
Me.Button4.Text = "删除临时文件"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(128, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly = True
Me.TextBox2.Size = New System.Drawing.Size(296, 22)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = ""
'
'StatusBar1
'
Me.StatusBar1.Location = New System.Drawing.Point(0, 168)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(440, 22)
Me.StatusBar1.TabIndex = 6
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.BackColor = System.Drawing.SystemColors.ControlLight
Me.ClientSize = New System.Drawing.Size(440, 190)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.StatusBar1, Me.TextBox2, Me.Button4, Me.Button3, Me.Button2, Me.TextBox1, Me.Button1})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "临时文件(temporary file)"
Me.ResumeLayout(False)
End Sub
#End Region
'取得暂存盘路径
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'清除 Status Bar
StatusBar1.Text = ""
'暂存盘路径
Dim tempPath As String = ""
'取得暂存盘路径
Try
tempPath = Path.GetTempPath()
Catch s1 As Security.SecurityException
StatusBar1.Text = "无权取得暂存盘路径. " & s1.Message
Catch e1 As Exception
StatusBar1.Text = "无法取得暂存盘路径. " & e1.Message
End Try
'显示暂存盘路径
TextBox1.Text = tempPath
End Sub
'临时文件档名
Dim tempFile As String = ""
'取得暂存盘路径文件名
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'清除 Status Bar
StatusBar1.Text = ""
Try
'取得暂存盘路径文件名
tempFile = Path.GetTempFileName()
'建立 FileInfo object 以设定暂存盘的属性, 不一定需要设定, 设定可以保留于快取区(cache), 以最佳化
Dim myFileInfo As New FileInfo(tempFile)
myFileInfo.Attributes = FileAttributes.Temporary
Catch e1 As Exception
StatusBar1.Text = "无法取得暂存盘路径文件名或设定属性. " & e1.Message
End Try
'显示暂存盘路径文件名
TextBox2.Text = tempFile
End Sub
'写读临时文件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'检查暂存盘是否已经建立
If tempFile = "" Then
StatusBar1.Text = "临时文件尚未建立!!!"
Return
Else
StatusBar1.Text = ""
End If
'写入一些文字到暂存盘
Try
'写入临时文件
Dim myWriter As StreamWriter = File.AppendText(tempFile)
myWriter.WriteLine("活在当下!")
myWriter.Flush()
myWriter.Close()
'读取临时文件
Dim myReader As StreamReader = File.OpenText(tempFile)
'显示暂存盘文字
StatusBar1.Text = "读取暂存盘文字: " + myReader.ReadToEnd()
myReader.Close()
Catch exc As Exception
StatusBar1.Text = "无法写入或读取临时文件."
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'检查暂存盘是否已经建立
If tempFile = "" Then
StatusBar1.Text = "临时文件尚未建立!!!"
Return
Else
StatusBar1.Text = ""
End If
'删除临时文件
Try
File.Delete(tempFile)
tempFile = ""
TextBox2.Text = ""
Catch e1 As Exception
StatusBar1.Text = "无法删除临时文件. " & e1.Message
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -