Share abrir um arquivo e tirar print vb.net

huyha839

New member
## abrir um arquivo e tirar in vb.net

** Cách mở tệp và in trong vb.net **

Hướng dẫn này sẽ chỉ cho bạn cách mở một tệp và in nó trong vb.net.Chúng tôi sẽ sử dụng lớp `System.io.file` để mở tệp và lớp` system.drawing.printing.printdocument` để in nó.

** 1.Mở tập tin**

Để mở một tệp, bạn có thể sử dụng phương thức `file.open ()`.Phương thức này lấy hai tham số: đường dẫn đến tệp và chế độ bạn muốn mở tệp.Chế độ có thể là `open` hoặc` openread`.

`` `VBNet
File Dim As io.fileStream = io.file.open ("C: \ myfile.txt", io.filemode.open)
`` `

** 2.Đọc tệp **

Khi bạn đã mở tệp, bạn có thể đọc nội dung của nó bằng phương thức `fileStream.read ()`.Phương thức này lấy một bộ đệm làm tham số và đọc nội dung của tệp vào bộ đệm.

`` `VBNet
Dim bộ đệm như byte () = new byte (file.length) {}
file.read (bộ đệm, 0, bộ đệm.length)
`` `

** 3.In tệp **

Để in tệp, bạn có thể sử dụng phương thức `printDocument.print ()`.Phương thức này lấy một đối tượng `printDocumentEventArgs` làm tham số.Đối tượng `printDocumentEventArgs` chứa thông tin về máy in và công việc in.

`` `VBNet
Dim PrintDocument dưới dạng in.
printdocument.print (in mới.printdocumenteventargs ())
`` `

**4.Đóng tệp **

Khi bạn đã in xong tệp, bạn nên đóng nó bằng phương thức `filestream.close ()`.

`` `VBNet
file.close ()
`` `

** Mã hoàn chỉnh **

Mã sau đây hiển thị mã hoàn chỉnh để mở tệp và in nó trong vb.net.

`` `VBNet
Nhập khẩu Hệ thống.io
Nhập hệ thống.Drawing.printing

Lớp công khai Mẫu1

Nút phụ riêng1_click (người gửi dưới dạng đối tượng, e là EventArgs) Nút tay cầm1.Click
' Mở tập tin.
File Dim As io.fileStream = io.file.open ("C: \ myfile.txt", io.filemode.open)

'Đọc tệp.
Dim bộ đệm như byte () = new byte (file.length) {}
file.read (bộ đệm, 0, bộ đệm.length)

'In tập tin.
Dim PrintDocument dưới dạng in.
printdocument.print (in mới.printdocumenteventargs ())

'Đóng tệp.
file.close ()
Kết thúc phụ

Kết thúc lớp học
`` `

## hashtags

* #vb.net
* #file I/O
* #In
* #tutorial
* #Programming
=======================================
## Abrir Um Arquivo E Tirar Print VB.NET

**How to Open a File and Print in VB.NET**

This tutorial will show you how to open a file and print it in VB.NET. We will use the `System.IO.File` class to open the file and the `System.Drawing.Printing.PrintDocument` class to print it.

**1. Open the file**

To open a file, you can use the `File.Open()` method. This method takes two parameters: the path to the file and the mode in which you want to open the file. The mode can be either `Open` or `OpenRead`.

```vbnet
Dim file As IO.FileStream = IO.File.Open("C:\MyFile.txt", IO.FileMode.Open)
```

**2. Read the file**

Once you have opened the file, you can read its contents using the `FileStream.Read()` method. This method takes a buffer as a parameter and reads the contents of the file into the buffer.

```vbnet
Dim buffer As Byte() = New Byte(file.Length) {}
file.Read(buffer, 0, buffer.Length)
```

**3. Print the file**

To print the file, you can use the `PrintDocument.Print()` method. This method takes a `PrintDocumentEventArgs` object as a parameter. The `PrintDocumentEventArgs` object contains information about the printer and the print job.

```vbnet
Dim printDocument As Printing.PrintDocument = New Printing.PrintDocument()
printDocument.Print(New Printing.PrintDocumentEventArgs())
```

**4. Close the file**

Once you have finished printing the file, you should close it using the `FileStream.Close()` method.

```vbnet
file.Close()
```

**Complete Code**

The following code shows the complete code for opening a file and printing it in VB.NET.

```vbnet
Imports System.IO
Imports System.Drawing.Printing

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Open the file.
Dim file As IO.FileStream = IO.File.Open("C:\MyFile.txt", IO.FileMode.Open)

' Read the file.
Dim buffer As Byte() = New Byte(file.Length) {}
file.Read(buffer, 0, buffer.Length)

' Print the file.
Dim printDocument As Printing.PrintDocument = New Printing.PrintDocument()
printDocument.Print(New Printing.PrintDocumentEventArgs())

' Close the file.
file.Close()
End Sub

End Class
```

## Hashtags

* #vb.net
* #file I/O
* #printing
* #tutorial
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top