-3

嘿,伙计们,在我只是隐藏父表单之前,但是现在当我尝试从父文件中读取时,它说它不能,因为它已经在一个进程中运行。我遵循了一些教程,它说要转到项目属性并在所有表单关闭时让应用程序停止运行。

但是现在既然我这样做了,它说找不到目录可能是因为我正在从父表单读取输入。无论如何,这是我的代码

Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))

我该怎么做呢?

编辑:

    Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs) Handles btnHunter.Click
    selection = "Hunter"
    writeData.classSelection()
End Sub

这就是单击按钮时我所拥有的。

这是classSelection子:

    Public Sub classSelection()
    If frmClass.selection = "Hunter" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    If frmClass.selection = "Gatherer" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    If frmClass.selection = "Farmer" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    writeFile1.Close()
End Sub

错误指向这一行:

If frmClass.selection = "Hunter" Then

说找不到文件路径的一部分。

4

3 回答 3

0

如果你想以封闭的父形式阅读输入文本框,你必须声明 public var

在你的项目中创建一个新模块..并添加这个

public sLogin as String

在您隐藏或关闭 frmLogin .. 添加之前

sLogin =  txtUser.Text

所以,你可以改变你的代码

Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin))
于 2013-05-23T04:31:43.493 回答
0

matzone 给了你一个很好的提示。要准确检查您的路径,只需使用变量添加 MessageBox :

Dim writePath1 As String
Dim writeFile1 As StreamWriter

writePath1 = "C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin
If MessageBox.Show(writePath1, "Continue ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
    writeFile1 = New StreamWriter(File.OpenWrite(writePath1))
    ' ...
    writeFile1.Close() ' Very important ! Adrian pointed it out.
End If

^^ 如果它有效,您可以放弃 Dialog 测试或用一些测试代码替换它,例如 If File.Exists(...)

但是,我不明白您是要关闭父表单还是隐藏它。这不一样 !关闭父表单将放弃对父表单成员的任何访问,包括txtUser.Text

如果要关闭父窗体,则 ChildForm 不应是您要关闭的父窗体的子窗体,或者您必须隐藏父窗体:

frmLogin.Hide() ' Not frmLogin.Close()

如果关闭 frmLogin,frmLogin.txtUser 将无法访问,或者使用 matzone 提供的 sLogin 代替。或者,您应该将 frmLogin.txtUser.Text 值传递给 ChildForm 的自定义属性。

Imports System.IO
Public Partial Class ChildForm1
    ' Inherits System.Windows.Form
    ' ...
    Private _txtUserFile As String

    Public WriteOnly Property TxtUserFile() As String
        Set(ByVal NewFileName As String)
            _txtUserFile = NewFileName
        End Set
    End Property

    Public Sub LoadFile()
        Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & txtUserFile))
        ' ...
        writeFile1.Close()
    End sub
    ' ...
End Class

然后在父 Form 中使用它:

MyChildForm.TxtUserFile = Me.txtUser.Text
' Me.Close() ' This will definately KILL Form1 (the parent one)
Me.Hide() ' Always use Hide() until you'll terminate your Application
MyChildForm.Show()
MyChildForm.LoadFile()

^^ 但这也不是一个好的代码!你的问题仍然不清楚(至少对我来说)“仍然说它找不到路径的一部分”,然后检查路径..

  • 该文件是否真的存在?
  • 路径是否包含故障?(使用提供的 MessageBox 测试)
  • 您的帐户可以访问该目录吗?(Windows 配置和帐户级别)
于 2013-05-23T08:46:16.317 回答
0

出色地 !

事实上,问题可能出在其他地方。

例如,我能够通过提供一个空字符串 [""] 作为以下任一值的值来重现您的异常:

frmLogin.txtUser.Text ' = ""
' or 
sLogin ' = ""
' or
txtUserFile ' = ""

事实上,我得到“找不到路径的一部分... ”异常,因为 StreamWriter 无法读取/写入文件,因为我没有为该文件提供有效的文件名。由于文件名参数是一个空字符串“”,因此为 StreamWriter 提供的路径只是表示一个目录而不是一个文件,并引发了异常。

现在,您应该在构建 StreamWriter 的新实例之前检查您是否有有效路径,以确保您实际上指向 File ?

Dim writeFile1 As StreamWriter
Dim MyEntirePath As String = "C:\Users\...\Accounts\" + frmLogin.txtUser.Text
MessageBox.Show(MyEntirePath) ' would be enough to be sure your path is correct

' Some test code here...
If everythingOK then ' create the StreamWriter...
    writeFile1 = New StreamWriter(MyEntirePath)
    ' ...
' ...

此外,创建流编写器并在代码的另一部分/方法中使用它也不是一个好主意。你永远不知道是否有一天,你会改变你的代码,而忘记在它们之间建立链接

Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))

' plus
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs)
    ...
End Sub

' plus
Public Sub classSelection()
    ...
    writeFile1.Close()
End Sub

^^ 太多“这里那里”...

如果您尝试单击 btnHunter 两次,您显然也会遇到异常。我不知道您的代码的目的是什么,也不知道它是如何工作的,它看起来像一个游戏。但我会使用 File.Exist(. .) 检查,如果没有,则在之前创建文件,并将其放入 Try/Catch 以检查我是否最终没有管理员权限来写入该目录。否则,制作一个允许用户将文件读/写到自定义文件夹的代码。而且,你有:

Application.StartupPath

^^ 非常有用,例如:

Dim MyFilePath As String = Application.StartupPath + "\Datas\MyText.txt"

经过两周的编码后,我通常会忘记将那些“C:\blabla..”或“D:\gnagna\”放在哪里,或者哪些类实际使用了这些绝对引用路径。自从我在另一台计算机上移至 Win7 的那一天起,我很久以前就放弃了这种获取目录的方式,而我使用这种方法开发的所有此类应用程序都注定要失败......

于 2013-06-14T00:07:47.737 回答