如何创建文本文件和编写文本 [英] how to create a text file and write texts

查看:106
本文介绍了如何创建文本文件和编写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计..我在我创建的文本文件上插入文本时遇到问题..

这是我的代码:



Dim file As String = Application.StartupPath&\&test.txt

Dim mystring As String =hello world



如果是System.IO.File.Exists(文件)那么

否则

'创建文本文件并编写默认文本

尝试

System.IO.File.Create(file)

Dim sw As New System.IO.StreamWriter(file)



sw.Write(mystring)

sw.Close()

MessageBox.Show(adto nah)

Catch ex As Exception

MessageBox.Show(ex.Message)

结束尝试

结束如果





问题说...无法打开文件,因为它被用于另一个进程。 。

代码在文本文件已经创建时工作正常..但是当我尝试创建它并向其插入文本时...代码不起作用..它最终只有和错误消息..对不起我的英语不好..

hi guys.. im having problems on inserting text on a textfile that i have created..
here's my code:

Dim file As String = Application.StartupPath & "\" & "test.txt"
Dim mystring As String = "hello world"

If System.IO.File.Exists(file) Then
Else
'create textfile and write default texts
Try
System.IO.File.Create(file)
Dim sw As New System.IO.StreamWriter(file)

sw.Write(mystring)
sw.Close()
MessageBox.Show("adto nah")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If


the problems says that..cannot open file because its being used on another process..
the code works fine when text file is already created.. but when i try to create it and insert text to it.. the code do not work.. it only ends up with and error msg.. sorry for my bad english..

推荐答案

最简单的方法是这样做:

Simplest way is to do just this:
File.WriteAllText(Application.StartupPath & "\" & "test.txt", "Hello World")

这几乎可以完成你的代码所做的一切......

That does pretty much everything your code does...


这篇关于如何创建文本文件和编写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆