您是否知道另一种保存和检索用户输入文本的方法?

时间:2017-08-27 16:50:07

标签: c# wpf vb.net xaml

这些是XAML代码:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="164" Width="302">
<Grid>
    <TextBox Height="23"  Margin="10,12,0,0" Name="TextBox1" VerticalAlignment="Top" Width="133" />
    <Button Content="Store Text" Height="23"  Margin="10,49,0,0" Name="Button1" VerticalAlignment="Top" Width="133" />
    <Button Content="Retrieved Stored Text" Height="23" Margin="10,88,0,0" Name="Button2" VerticalAlignment="Top" Width="133" />
</Grid>
</Window>

这些是vb.net代码:

Class MainWindow 

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    My.Settings.mySett = TextBox1.Text
    My.Settings.Save()
End Sub


Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
    MessageBox.Show(My.Settings.mySett)
End Sub

End Class

这是Screen Shot

这是我的问题:

我已经在使用上面的代码来保存和检索用户输入文本。

但是当我的应用程序移动到另一个目录时,此方法不起作用。

我的意思是如果用户将我的应用程序移动到另一个目录,则设置将丢失。

那么,还有其他方法可以保存和检索用户输入文本吗?

3 个答案:

答案 0 :(得分:0)

如果您的应用程序不是独立的,我建议您在Application.StartupPath创建一个文件,并在加载表单时加载文本。这样,无论用户在哪里保存应用程序(即快捷方式),文件都将始终位于已安装的目录中。

这总是有效的。

答案 1 :(得分:0)

您可以将文本保存在Temp文件夹中,并像以下

一样进行检索
class ViewController: UIViewController {
    var child: ChildViewController?

    @IBAction func open(_ sender: Any) {
        performSegue(withIdentifier: "segueChild", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "segueChild") {
             child = segue.destination as? ChildViewController
            child?.parentVC = self
        }
    }
}

class ChildViewController: UIViewController {
    var parentVC: ViewController?

    @IBAction func close(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
}

答案 2 :(得分:0)

您也可以使用Registry来保存值。

con=database('databaseName','root','');
colnames={c2}
data={mat2str(c2)}
datainsert(con,tablename,colnames,data)
close(con)