Share vb.net getfiles

quangthang561

New member
### Cách nhận tệp trong vb.net

** 1.Nhận tệp từ một thư mục **

Để nhận các tệp từ một thư mục trong vb.net, bạn có thể sử dụng phương thức `Directory.getFiles ()`.Phương thức này lấy hai tham số: đường dẫn đến thư mục và chuỗi bộ lọc.Chuỗi bộ lọc có thể được sử dụng để chỉ định các tệp nào sẽ bao gồm hoặc loại trừ.Ví dụ: mã sau sẽ nhận tất cả các tệp từ thư mục hiện tại có phần mở rộng ".txt":

`` `VBNet
Dim File As String () = Directory.getFiles (".", "*.Txt")
`` `

Sau đó, bạn có thể lặp lại thông qua mảng `file` để truy cập vào từng tệp.Ví dụ:

`` `VBNet
Đối với mỗi tệp trong tệp
'Làm gì đó với tệp
Kế tiếp
`` `

** 2.Nhận tệp từ ổ đĩa **

Để nhận các tệp từ ổ đĩa trong vb.net, bạn có thể sử dụng phương thức `driveInfo.getDrives ()`.Phương thức này trả về một mảng của các đối tượng `DriveInfo`, mỗi đối tượng đại diện cho một ổ đĩa trên hệ thống.Sau đó, bạn có thể sử dụng thuộc tính `driveInfo.rootDirectory` để đưa đường dẫn đến thư mục gốc của ổ đĩa.Ví dụ: mã sau sẽ nhận tất cả các tệp từ thư mục gốc của ổ C:

`` `VBNet
Dim Files as String () = Directory.getFiles (DriveInfo.GetDries () [0] .rootDirectory)
`` `

** 3.Nhận tệp từ chia sẻ mạng **

Để nhận các tệp từ chia sẻ mạng trong vb.net, bạn có thể sử dụng phương thức `network.share.getshares ()`.Phương thức này trả về một mảng của các đối tượng `networkShare`, mỗi đối tượng đại diện cho một chia sẻ mạng trên hệ thống.Sau đó, bạn có thể sử dụng thuộc tính `networkShare.path` để đưa đường dẫn đến chia sẻ mạng.Ví dụ: mã sau sẽ nhận tất cả các tệp từ mạng \ server \ chia sẻ:

`` `VBNet
Dim Files as String () = Directory.getFiles (network.share.getShares () [0] .Path)
`` `

**4.Nhận tệp từ URL **

Để nhận các tệp từ một URL trong VB.NET, bạn có thể sử dụng phương thức `WebClient.doLoadFile ()`.Phương thức này lấy hai tham số: URL của tệp và đường dẫn đến tệp cục bộ.Ví dụ: mã sau sẽ tải xuống tệp tại `https: // www.example.com/file.txt` vào tệp cục bộ` c: \ temp \ file.txt`:

`` `VBNet
WebClient.DoadFile ("https://www.example.com/file.txt", "C: \ temp \ file.txt")
`` `

** 5.Nhận tệp từ luồng **

Để nhận các tệp từ một luồng trong vb.net, bạn có thể sử dụng phương thức `stream.read ()`.Phương thức này lấy một mảng byte làm tham số và đọc dữ liệu từ luồng vào mảng.Sau đó, bạn có thể sử dụng phương thức `mảng.tofile ()` để ghi dữ liệu từ mảng sang tệp.Ví dụ: mã sau sẽ đọc dữ liệu từ luồng và ghi nó vào một tệp:

`` `VBNet
Dim Stream As Stream = ...
Dim byte () như byte = new byte (stream.length - 1) {}
stream.read (byte, 0, byte.length)
File.writeallbytes ("C: \ temp \ file.txt", byte)
`` `

### hashtags

* #vb.net
* #tài liệu
* #io
* #Programming
* #phát triển
=======================================
### How to Get Files in VB.NET

**1. Get Files from a Folder**

To get files from a folder in VB.NET, you can use the `Directory.GetFiles()` method. This method takes two parameters: the path to the folder and a filter string. The filter string can be used to specify which files to include or exclude. For example, the following code will get all files from the current directory that have the extension ".txt":

```vbnet
Dim files As String() = Directory.GetFiles(".", "*.txt")
```

You can then iterate through the `files` array to access each file. For example:

```vbnet
For Each file In files
' Do something with the file
Next
```

**2. Get Files from a Drive**

To get files from a drive in VB.NET, you can use the `DriveInfo.GetDrives()` method. This method returns an array of `DriveInfo` objects, each of which represents a drive on the system. You can then use the `DriveInfo.RootDirectory` property to get the path to the root directory of the drive. For example, the following code will get all files from the root directory of the C drive:

```vbnet
Dim files As String() = Directory.GetFiles(DriveInfo.GetDrives()[0].RootDirectory)
```

**3. Get Files from a Network Share**

To get files from a network share in VB.NET, you can use the `Network.Share.GetShares()` method. This method returns an array of `NetworkShare` objects, each of which represents a network share on the system. You can then use the `NetworkShare.Path` property to get the path to the network share. For example, the following code will get all files from the \\server\share network share:

```vbnet
Dim files As String() = Directory.GetFiles(Network.Share.GetShares()[0].Path)
```

**4. Get Files from a URL**

To get files from a URL in VB.NET, you can use the `WebClient.DownloadFile()` method. This method takes two parameters: the URL of the file and the path to the local file. For example, the following code will download the file at `https://www.example.com/file.txt` to the local file `C:\temp\file.txt`:

```vbnet
WebClient.DownloadFile("https://www.example.com/file.txt", "C:\temp\file.txt")
```

**5. Get Files from a Stream**

To get files from a stream in VB.NET, you can use the `Stream.Read()` method. This method takes a byte array as a parameter and reads data from the stream into the array. You can then use the `Array.ToFile()` method to write the data from the array to a file. For example, the following code will read data from a stream and write it to a file:

```vbnet
Dim stream As Stream = ...
Dim bytes() As Byte = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
File.WriteAllBytes("C:\temp\file.txt", bytes)
```

### Hashtags

* #vb.net
* #file
* #io
* #Programming
* #development
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top