VB.Net by API/System.IO/File

Материал из VB Эксперт
Перейти к: навигация, поиск

File.AppendText

  
Imports System.IO

Public Class Tester
    Public Shared Sub Main
        Dim w As StreamWriter
        w = File.AppendText("test.txt")
        w.Write("it is writing data")
        w.WriteLine(1)
        w.WriteLine("writeline")
        w.WriteLine("True")
        w.Close()
    End Sub
End Class


File.Copy

  
 
Imports System.Windows.Forms
public class FileOpenDialogFilter
   public Shared Sub Main
      System.IO.File.Copy("c:\\test.txt", "c:\\test1.txt", True)
   End Sub
End class


File.CreateText

  
Imports System
Imports System.IO
public class LogTest
   public Shared Sub Main
        MyLog.Log("Started Up", "C:\test.txt")
        MyLog.Log(String.Format("DisplayName called, {0} {1} output","B", "A"), "C:\test.txt")
   End Sub
End class
Public Class MyLog
    Const DefaultLogFile As String = "C:\LogFile.txt"
    Shared Sub Log(ByVal Message As String,ByVal FileName As String)
        Dim objLogFile As StreamWriter
        
        If IO.File.Exists(FileName) Then
            objLogFile = File.AppendText(FileName)
        ElseIf Directory.Exists (Path.GetDirectoryName(FileName)) Then
            objLogFile = File.CreateText(FileName)
        Else
            objLogFile = File.AppendText(DefaultLogFile)
        End If
        objLogFile.WriteLine(Message)
        objLogFile.Flush()
        objLogFile.Close()
    End Sub
End Class


File.Delete

  
Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim f As File
        f.Delete("test.txt")
    End Sub
End Class


File.Exists

  
Imports System.IO
Public Class Tester
    Public Shared Sub Main
    
         Console.WriteLine(File.Exists("test.vb"))

    End Sub
End Class


File.GetCreationTime

  
Imports System.IO
Public Class Tester
    Public Shared Sub Main
    
         Console.WriteLine(File.GetCreationTime("test.vb"))
         Console.WriteLine(File.GetLastWriteTime("test.vb"))
         Console.WriteLine(File.GetLastAccessTime("test.vb"))
    End Sub
End Class


File.GetLastAccessTime

  
Imports System.IO
Public Class Tester
    Public Shared Sub Main
    
         Console.WriteLine(File.GetCreationTime("test.vb"))
         Console.WriteLine(File.GetLastWriteTime("test.vb"))
         Console.WriteLine(File.GetLastAccessTime("test.vb"))
    End Sub
End Class


File.GetLastWriteTime

  
Imports System.IO
Public Class Tester
    Public Shared Sub Main
    
         Console.WriteLine(File.GetCreationTime("test.vb"))
         Console.WriteLine(File.GetLastWriteTime("test.vb"))
         Console.WriteLine(File.GetLastAccessTime("test.vb"))
    End Sub
End Class


File.Move

  
 
Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim f As File
        f.Move("test.txt", "text2.txt")
    End Sub
End Class


File.OpenRead

  
Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System.Runtime.Serialization.Formatters
public class MainClass
   Shared Public Sub Main(ByVal CmdArgs() As String)
        If (CmdArgs.Length <> 1) Then
            Console.WriteLine("usage: TestKeyHash <filename>")
            End
        End If
        Dim key() As Byte = Encoding.ASCII.GetBytes("My Secret Key".ToCharArray())
        Dim hmac As HMACSHA1 = New HMACSHA1(key)
        Dim fs As FileStream = File.OpenRead(CmdArgs(0))
        Dim hash() As Byte = hmac.ruputeHash(fs)
        Dim b64 As String = Convert.ToBase64String(hash)
        fs.Close()
        Console.WriteLine(b64)
    End Sub
End Class