简体   繁体   English

在VB.NET中创建和写入文件>启动应用程序>删除文件

[英]Create and write to file > start application > delete file in VB.NET

I'm trying to create a VB.NET application which writes multiple lines of text into a text file, then starts an application and after the application started, deletes the text file. 我正在尝试创建一个VB.NET应用程序,该应用程序将多行文本写入一个文本文件,然后启动一个应用程序,并在应用程序启动后删除该文本文件。

How exactly can I realize that? 我到底怎么能意识到?

-- -

Edit: 编辑:

I now got this code: 我现在得到以下代码:

    Dim iniFile As String = Application.StartupPath + "\settings.ini"

    If System.IO.File.Exists(iniFile) = True Then
        File.Delete(iniFile)
    End If

    If System.IO.File.Exists(iniFile) = False Then
        File.Create(iniFile)
    End If

    Dim fileStr As String() = {"line1", "line2", "line3"}
    File.WriteAllLines(iniFile, fileStr)

    Dim p As Process = Process.Start(Application.StartupPath + "\app.exe")
    p.WaitForInputIdle()

    If System.IO.File.Exists(iniFile) = True Then
        File.Delete(iniFile)
    End If

The only problem I got, is that VS is telling me, the file is in use. 我得到的唯一问题是VS告诉我该文件正在使用中。 Between creating and editing the file. 在创建和编辑文件之间。 Any ideas for that? 有什么想法吗?

Your code is starting the app and then moving straight on to delete the ini file. 您的代码正在启动应用程序,然后直接删除ini文件。

You need to wait for the process to exit first before you continue with deleting the ini file 您需要先等待进程退出,然后再继续删除ini文件

Eg 例如

 {code to create ini file} 'Start the process. Dim p As Process = Process.Start(Application.StartupPath + "\\app.exe") 'Wait for the process window to complete loading. p.WaitForInputIdle() 'Wait for the process to exit. p.WaitForExit() {code to delete ini file} 

Full example here: https://support.microsoft.com/en-us/kb/305368 此处的完整示例: https : //support.microsoft.com/en-us/kb/305368

Just use File.WriteAllText 只需使用File.WriteAllText
Note: As others already mentioned, you should check against True in your last If 注意:正如其他人已经提到的,您应该在最后一个If检查True

If System.IO.File.Exists(Application.StartupPath + "\settings.ini") = False Then
    File.Create(Application.StartupPath + "\settings.ini")
End If

Dim fileStr As String() = {"line1", "line2", "line3"}
System.IO.File.WriteAllText(Application.StartupPath + "\settings.ini", [String].Join(Environment.NewLine, fileStr))

Process.Start(Application.StartupPath + "\app.exe")

If System.IO.File.Exists(Application.StartupPath + "\settings.ini") = True Then
    File.Delete(Application.StartupPath + "\settings.ini")
End If

Use File.WriteAllLines since it 自从使用File.WriteAllLines

Creates a new file, write the specified string array to the file, and then closes the file. 创建一个新文件,将指定的字符串数组写入该文件,然后关闭该文件。 [...] If the target file already exists, it is overwritten. [...]如果目标文件已经存在,它将被覆盖。 msdn msdn

Also you should use Path.Combine to setup your path 另外,您应该使用Path.Combine设置路径

Dim path as String = Path.Combine(Application.StartupPath, "settings.ini")
Dim fileStr As String() = {"line1", "line2", "line3"}
File.WriteAllLines(path, fileStr)

Use the Path.Combine for the Process.Start and File.Delete too. 也将Path.Combine用于Process.StartFile.Delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM