.net 如何创建和打开文件文档

.net 如何创建和打开文件文档,.net,vb.net,file,console-application,.net,Vb.net,File,Console Application,我有以下方法/程序 Private Sub CreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String) Dim file2create = directory & filename & extension Console.WriteLine(file2create) If (Not File.Exists(file2create))

我有以下方法/程序

Private Sub CreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String)

    Dim file2create = directory & filename & extension
    Console.WriteLine(file2create)
    If (Not File.Exists(file2create)) Then
        File.Create(file2create)
    Else
        File.Open(file2create, FileMode.Open)
    End If
    console.ReadLine()
End Sub
这段代码成功地创建了一个文件,但未能打开它,我只是说,我已经用类似的问题检查了其他答案,但回答没有解决我的问题,我在这里做错了什么。谢谢

=============================================更新====================================

谢谢大家的回答,但文件还没打开。我只是想澄清一下,当我说“打开”时,我指的是物理上转到文件并单击它的行为

如果我使用这个代码


该文件将成功打开,但如果我使用file.open()文件将无法打开,因此本质上,我希望创建一个文件,然后以与上述代码相同的方式打开该文件。

您必须将
文件移动到
if..then
块之后

在这里:


以下是正确的方法,可以在文件不存在时创建文件,然后使用
file.open
方法打开它,该方法返回一个对象:

如果您只是想创建一个文件,并在其中写入一些文本以覆盖现有内容,您可以使用:

File.WriteAllText(file2create, "SomethingToWrite")

希望这能有所帮助。

您不是只想将
文件.Open
移到if语句之外吗?或者将整个if语句替换为
文件.Open(file2create,FileMode.OpenOrCreate)
返回一个
文件流
,该文件流将锁定其他代码/进程无法访问该文件。这是您想要的还是您打算使用此代码实际读取/写入它?否则您应该处理流:
File.Open(file2create,FileMode.OpenOrCreate).dispose()
@AhmedAbdelhameed如果您可以发布OPs代码的更新版本作为答案,我肯定会优先于其他当前答案+1。因此,您希望执行该文件或其关联的应用程序,那么您已经找到的代码就应该执行该操作
File.Open()
不会执行该文件,但会打开一个文件,以便您可以从中读取数据,这就是为什么它不能像您想象的那样工作。:)应该这样做。谢谢看着你的代码,我也学到了一些东西。。Path.Combine。现在要用它来更新大量代码。如果我这样做,我会出现以下错误---->System.IO.IOException:“进程无法访问文件'C:\Users\jamisco\Documents\jbiadfsdipa.txt',因为它正被另一个进程使用。”?哈哈,好的。见我的最新答案我现在正在质疑我对open的定义现在见我的答案
 Private Sub CreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String)
    Dim file2create = directory & filename & extension
    Console.WriteLine(file2create)
    If (Not File.Exists(file2create)) Then
        File.Create(file2create)
    End If
   'if file exits then open the file
If System.IO.File.Exists(file2create) = True Then
    Process.Start(file2create)
Else
    MsgBox("File Does Not Exist")
End If
End Sub
Private Sub OpenOrCreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String)
    'Use `Path.Combine` so you don't have to worry if the
    'directory path ends with a "\" or not.
    Dim file2create = Path.Combine(directory, filename) & extension
    Console.WriteLine(file2create)

    'Use a `Using` statement to make sure the FileStream object gets
    'disposed, and to prevent the file from staying locked after
    'finishing what you want to do with it.
    Using fs As FileStream = File.Open(file2create, FileMode.OpenOrCreate)
        'I'm not sure what you'd like to do after opening the file,
        'but now you have a `FileStream` object which you can use
        'to write bytes to the file (i.e. using `fs.Write()`).
    End Using

    Console.ReadLine()
End Sub
File.WriteAllText(file2create, "SomethingToWrite")