使用 VB.NET 创建一个新的 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4165829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:08:17  来源:igfitidea点击:

Create a new txt file using VB.NET

vb.net

提问by sam

How do I create an empty .txtfile in the location C:\my files\2010using VB.NET?

如何使用 VB.NET.txt在该位置创建一个空文件C:\my files\2010

回答by Schenz

Here is a single line that will create (or overwrite) the file:

这是将创建(或覆盖)文件的一行:

File.Create("C:\my files10\SomeFileName.txt").Dispose()

Note: calling Dispose() ensures that the reference to the file is closed.

注意:调用 Dispose() 可确保关闭对文件的引用。

回答by Geoffrey

You also might want to check if the file already exists to avoid replacing the file by accident (unless that is the idea of course:

您可能还想检查文件是否已经存在以避免意外替换文件(当然除非这是想法:

Dim filepath as String = "C:\my files10\SomeFileName.txt"
If Not System.IO.File.Exists(filepath) Then
   System.IO.File.Create(filepath).Dispose()
End If

回答by Human Wannabe

You can try writing into the Documents folder. Here is a "debug" function I did for the debugging needs of my project:

您可以尝试写入 Documents 文件夹。这是我为项目的调试需求所做的“调试”功能:

Private Sub writeDebug(ByVal x As String)
    Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Dim FILE_NAME As String = path & "\mydebug.txt"
    MsgBox(FILE_NAME)
    If System.IO.File.Exists(FILE_NAME) = False Then
        System.IO.File.Create(FILE_NAME).Dispose()
    End If
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
    objWriter.WriteLine(x)
    objWriter.Close()
End Sub

There are more standard folders you can access through the "SpecialFolder" object.

您可以通过“SpecialFolder”对象访问更多标准文件夹。

回答by Abdullah Gulraiz

open C:\myfile.txt for append as #1
write #1, text1.text, text2.text
close()

This is the code I use in Visual Basic 6.0. It helps me to create a txt file on my drive, write two pieces of data into it, and then close the file... Give it a try...

这是我在Visual Basic 6.0 中使用的代码。它帮助我在我的驱动器上创建一个txt文件,将两条数据写入其中,然后关闭文件......试试看......

回答by ABPerson

You could just use this

你可以用这个

FileOpen(1, "C:\my files10\SomeFileName.txt", OpenMode.Output)
FileClose(1)

This opens the file replaces whatever is in it and closes the file.

这将打开文件替换其中的任何内容并关闭文件。