VB.NET中如何选择保存打印机名称配置并重启后自动显示
VB.NET 打印机配置保存与自动加载实现
功能需求
在VB.NET中实现以下功能:选择打印机名称并将其配置保存为字符串,重新打开窗体时,之前选中的打印机名称能自动显示在表单上。
用户编写的相关代码及配置文件
窗体后台代码
Private Sub configurationfile() Dim reader As New System.Configuration.AppSettingsReader Dim PrinterName As String = reader.GetValue("PrinterName", GetType(String)).ToString().Replace(",", "") If Not String.IsNullOrEmpty(PrinterName) Then TextBox1.Text = PrinterName CheckBox1.Checked = True End If End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged Dim fullpath = Application.StartupPath + "\rawprinting.exe" Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(fullpath) config.AppSettings.Settings.Remove("PrinterName") If CheckBox1.Checked Then config.AppSettings.Settings.Add("PrinterName", TextBox1.Text) Else config.AppSettings.Settings.Add("PrinterName", "") End If '保存App.config文件中的修改 config.Save(ConfigurationSaveMode.Modified) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim printer As String = TextBox1.Text End Sub
App.config配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="PrinterName" value="Generic / Text Only" /> </appSettings> </configuration>
内容的提问来源于stack exchange,提问作者roy
相关产品推荐
相关产品推荐

