Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use VB.NET control to manipulate files

Shulou Source: shulou.com Published: 2022-06-02 08:55:25 10月04日 Update

This article mainly introduces how to use VB.NET control to operate files, the article is very detailed, has a certain reference value, interested friends must read it!

Show Fil

Because of the differences in the storage formats of different files, different files can be displayed correctly only by using appropriate methods. On the surface, that is, different file suffixes have different names, and different applications can only read and display files in one or more different formats. For example, files with the suffix .DOC can be opened with WORD, but cannot be displayed correctly with notepad. Therefore, VB provides us with different controls to display different files.

Here we take the text control as an example to see how to display files with it.

Private Sub mnuopen_Click () 'execute Dim filepath as Sting Dim strLine As String when opening a file on the menu' when no file is selected, text.text is empty character, does not display anything text1.text = "" CommonDialog1.FileName = "" 'the following statement sets the file filtering mode, and can display the txt file CommonDialog1.Filter = "(* .txt) | * .txt"' to establish a general dialog box for opening mode. You can also use commondialog1.showopen CommonDialog1.Action = 1 filepath=CommonDialog1.FileName'to get the selected file Open filepath For Input As # 1 to open the selected file Do Until EOF (1)'to display the open file Line Input # 1, strLine text1text1.Text = text1.Text + strLine + Chr (13) + Chr (10) Loop Close # 1'to close the open file End Sub

In addition to what comes with the VB.NET control, we can also use fileSystemObject file system objects, file objects, and files collections. Below we give two examples. For more details, please refer to the previous catalog section.

The following code illustrates how to get a file object and how to view one of its properties.

Sub ShowFileInfo (filespec) Dim fs, f, s Set fs = CreateObject ("scripting.FileSystemObject") Set f = fs.GetFile (filespec) s = f.DateCreated MsgBox s

File operation is a necessary function of every software. Using the common controls provided by Visual Basic, you can easily operate the file End Sub.

For how to get a files collection and how to use For Each... Next statement to access each file in this collection, we can refer to the following example:

Sub ShowFolderList (folderspec) 'folderspec is the folder name Dim fs, f, F1 S Set fs = CreateObject ("Scripting.FileSystemObject") Set f = fs.GetFolder (folderspec) 'get the folder object related to the folderspec folder Set ffc = f.Files' get the files collection For Each fi in fc'of the folder contained in the folder object access each folder ss= s & f1.name'in the folder collection format the text to be displayed ss= s & vbCrLf Next MsgBox s' display the information End Sub above is "such as" All the contents of the article "how to use VB.NET controls to manipulate files" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Files different controls objects content examples folders formats references selections suffixes dialogs methods more articles statements software dialogs appropriate two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info NVidia Linux vpn