VB.Net/File Directory/File — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Содержание
- 1 Change file attribute for *.*
- 2 Delete a file if exist
- 3 Delete *.Bak under a directory
- 4 English File Size
- 5 File Information
- 6 File Time: created, last modified and accessed
- 7 Find Hidden File under a directory
- 8 Get Files under an Directory and print its Full Name and Length
- 9 Get Invalid File Name Chars
- 10 Is it a File or a Directory
- 11 Update file create time, last access time and last write time
Change file attribute for *.*
Imports System.IO
Module Module1
Sub ProcessTree(ByVal Dir As String)
Dim DirObj As New DirectoryInfo(Dir)
Dim Files As FileInfo() = DirObj.GetFiles("*.*")
Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*")
Dim Filename As FileInfo
For Each Filename In Files
Try
If (Filename.Attributes And FileAttributes.ReadOnly) Then
Filename.Attributes = (Filename.Attributes And Not FileAttributes.ReadOnly)
End If
Catch E As Exception
Console.WriteLine("Error changing attribute for {0}", Filename.FullName)
Console.WriteLine("Error: {0}", E.Message)
End Try
Next
Dim DirectoryName As DirectoryInfo
For Each DirectoryName In Dirs
Try
ProcessTree(DirectoryName.FullName)
Catch E As Exception
Console.WriteLine("Error accessing {0}", DirectoryName.FullName)
Console.WriteLine("Error: {0}", E.Message)
End Try
Next
End Sub
Sub Main()
ProcessTree("C:\")
End Sub
End Module
Delete a file if exist
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Imports System.Drawing.Imaging
Imports System.Drawing
Public Class MainClass
Shared Sub Main()
Dim file_name As String = "test.wmf"
" Delete the file if it exists.
If Len(Dir$(file_name)) > 0 Then Kill(file_name)
End Sub
End Class
Delete *.Bak under a directory
Imports System.IO
Module Module1
Sub DeleteFiles(ByVal Dir As String)
Dim DirObj As New DirectoryInfo(Dir)
Dim Files As FileInfo() = DirObj.GetFiles("*.bak")
Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*")
Dim Filename As FileInfo
For Each Filename In Files
Try
File.Delete(Filename.FullName)
Console.WriteLine("Deleted {0}", Filename.FullName)
Catch E As Exception
Console.WriteLine("Error deleting {0}", Filename.FullName)
Console.WriteLine("Error: {0}", E.Message)
End Try
Next
Dim DirectoryName As DirectoryInfo
For Each DirectoryName In Dirs
Try
DeleteFiles(DirectoryName.FullName)
Catch E As Exception
Console.WriteLine("Error accessing {0}", DirectoryName.FullName)
Console.WriteLine("Error: {0}", E.Message)
End Try
Next
End Sub
Sub Main()
DeleteFiles("C:\")
End Sub
End Module
English File Size
Public Class MainClass
Public Shared Function ConvertBytes(ByVal Bytes As Long) As String
If Bytes >= 1073741824 Then
Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") & " GB"
ElseIf Bytes >= 1048576 Then
Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
Return Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes > 0 And Bytes < 1024 Then
Return Fix(Bytes) & " Bytes"
Else
Return "0 Bytes"
End If
End Function
Public Shared Sub Main()
Dim objInfo As New System.IO.FileInfo("c:\a.dat")
System.Console.WriteLine(ConvertBytes(objInfo.Length))
End Sub
End Class
File Information
Imports System.IO
Imports System.Windows.Forms
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.txtDisplay = New System.Windows.Forms.TextBox
Me.cmdCreate = New System.Windows.Forms.Button
Me.cmdChangeDate = New System.Windows.Forms.Button
Me.cmdGetInfo = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
"
Me.txtDisplay.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDisplay.Location = New System.Drawing.Point(9, 79)
Me.txtDisplay.Multiline = True
Me.txtDisplay.ReadOnly = True
Me.txtDisplay.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtDisplay.Size = New System.Drawing.Size(280, 168)
Me.txtDisplay.TabIndex = 9
"
Me.cmdCreate.Location = New System.Drawing.Point(9, 39)
Me.cmdCreate.Size = New System.Drawing.Size(88, 24)
Me.cmdCreate.TabIndex = 8
Me.cmdCreate.Text = "Create"
"
Me.cmdChangeDate.Location = New System.Drawing.Point(201, 39)
Me.cmdChangeDate.Size = New System.Drawing.Size(88, 24)
Me.cmdChangeDate.TabIndex = 7
Me.cmdChangeDate.Text = "Change Date"
"
Me.cmdGetInfo.Location = New System.Drawing.Point(105, 39)
Me.cmdGetInfo.Size = New System.Drawing.Size(88, 24)
Me.cmdGetInfo.TabIndex = 6
Me.cmdGetInfo.Text = "Get Info"
"
Me.TextBox1.Location = New System.Drawing.Point(9, 11)
Me.TextBox1.Size = New System.Drawing.Size(280, 21)
Me.TextBox1.TabIndex = 5
Me.TextBox1.Text = "c:\myfile.txt"
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(298, 259)
Me.Controls.Add(Me.txtDisplay)
Me.Controls.Add(Me.cmdCreate)
Me.Controls.Add(Me.cmdChangeDate)
Me.Controls.Add(Me.cmdGetInfo)
Me.Controls.Add(Me.TextBox1)
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
Friend WithEvents cmdCreate As System.Windows.Forms.Button
Friend WithEvents cmdChangeDate As System.Windows.Forms.Button
Friend WithEvents cmdGetInfo As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
Try
Dim MyFile As New FileInfo("c:\myfile.txt")
MyFile.Create().Close()
Out("File created.")
Catch err As Exception
Out(err.Message)
End Try
End Sub
Private Sub cmdGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetInfo.Click
Try
Dim MyFile As New FileInfo("c:\myfile.txt")
Out("Length in bytes: " & MyFile.Length)
Out("Attribute list: " & MyFile.Attributes.ToString)
Out("Stored in: " & MyFile.DirectoryName)
Out("Created: " & MyFile.CreationTime)
Catch err As Exception
Out(err.Message)
End Try
End Sub
Public Sub Out(ByVal NewText As String) " Utility for displaying information.
txtDisplay.Text &= vbNewLine & NewText
txtDisplay.SelectionStart = txtDisplay.Text.Length - 1
txtDisplay.ScrollToCaret()
End Sub
Private Sub cmdChangeDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChangeDate.Click
Try
Dim MyFile As New FileInfo("c:\myfile.txt")
MyFile.LastWriteTime = DateTime.Today.Add(TimeSpan.FromDays(100))
Out("Time updated.")
Catch err As Exception
Out(err.Message)
End Try
End Sub
End Class
File Time: created, last modified and accessed
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim fileName As String
fileName = "test.vb"
Console.WriteLine( fileName & " exists" )
" output when file or directory was created
Console.WriteLine("Created : " & File.GetCreationTime(fileName) )
" output when file or directory was last modified
Console.WriteLine("Last modified: " & _
File.GetLastWriteTime(fileName) )
" output when file or directory was last accessed
Console.WriteLine("Last accessed: " & _
File.GetLastAccessTime(fileName) )
End Sub " Main
End Class
Find Hidden File under a directory
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim myDirectory As DirectoryInfo
myDirectory = New DirectoryInfo("C:\")
Dim aFile As FileInfo
For Each aFile In myDirectory.GetFiles
If (aFile.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
Console.WriteLine("FOUND hidden file named " & aFile.FullName)
End If
Next
End Sub
End Class
Get Files under an Directory and print its Full Name and Length
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim myDirectory As DirectoryInfo
myDirectory = New DirectoryInfo("C:\")
Dim aFile As FileInfo
For Each aFile In myDirectory.GetFiles
Console.WriteLine("The file named " & aFile.FullName & _
" has length " & aFile.Length)
Next
End Sub
End Class
Get Invalid File Name Chars
Imports System
Imports System.IO
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
For Each ch As Char In Path.GetInvalidFileNameChars()
Console.Write(ch & " ")
Next ch
End Sub
End Class
Is it a File or a Directory
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim fileName As String
fileName = "test.vb"
If File.Exists(fileName) Then
Console.WriteLine( "File")
ElseIf Directory.Exists(fileName) Then
Console.WriteLine( "Directory")
Else
Console.WriteLine( "File for dir does not exist")
End If
End Sub " Main
End Class
Update file create time, last access time and last write time
Imports System.IO
Module Module1
Sub Main()
Try
Dim FileObj As New FileInfo("test.dat")
FileObj.CreationTime = DateTime.Now
FileObj.LastAccessTime = DateTime.Now
FileObj.LastWriteTime = DateTime.Now
Console.WriteLine("test.dat date and time updated")
Catch Ex As Exception
Console.WriteLine("Error updating test.dat " & Ex.Message)
End Try
End Sub
End Module