vb.net 中with ..... end with 的用法

With...End With     允许对指定的对象执行一系列语句,而不必重新限定对象的名称。例如,若要修改单个对象的若干不同的属性,请将属性赋值语句放在 With...End With 中,这样只用引用一次对象,而不是在每个属性赋值中都引用它。

Directory.GetFileSystemEntries 方法 (String)返回指定目录中所有文件和子目录的名称。

Directory.GetFileSystemEntries (String) 返回指定目录中所有文件和子目录的名称。

Directory.GetFileSystemEntries (String, String) 返回与指定搜索条件匹配的文件系统项的数组。

 

 

从文件读取文本

方法一:

 Using sr As StreamReader = New StreamReader("TestFile.txt")
                Dim line As String
               Do
                    line = sr.ReadLine()
                    Console.WriteLine(Line)
                Loop Until line Is Nothing
                sr.Close()
            End Using

方法二:

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextFromFile
   
Private Const FILE_NAME As String = "MyFile.txt"
    Public Shared Sub Main()
       
If Not File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} does not exist.", FILE_NAME)
           
Return
        End If
        Using sr As StreamReader = File.OpenText(FILE_NAME)
           
Dim input As String
            input = sr.ReadLine()
           
While Not input Is Nothing
                Console.WriteLine(input)
                input = sr.ReadLine()
           
End While
            Console.WriteLine("The end of the stream has been reached.")
            sr.Close()
       
End Using
    End Sub
End Class

 

如何:向文件写入文本

方法一: 

Using sw As StreamWriter = New StreamWriter("TestFile.txt")
            sw.Write("This is the ")
            sw.WriteLine(
"header for the file.")
          
  sw.WriteLine(DateTime.Now)
            sw.Close()
       
End Using

方法二:

 Private Const FILE_NAME As String = "MyFile.txt"
    Public Shared Sub Main()
       
If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists.", FILE_NAME)
           
Return
        End If
        Using sw As StreamWriter = File.CreateText(FILE_NAME)
            sw.WriteLine(
"This is my file.")
            sw.WriteLine(
"I can write ints {0} or floats {1}, and so on.", 1, 4.2)
            sw.Close()
       
End Using
    End Sub



 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28311102/viewspace-749779/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28311102/viewspace-749779/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值