how to save printer name configuration to string in vb.net - vb.net

how to select and save the printer name configuration to a string. If I reopen then automatically the name of the printer I selected earlier it appears on the form.
Thanks
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
'Save the changes in App.config file.
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
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="PrinterName" value="Generic / Text Only" />
</appSettings>
</configuration>

According to the reference link below I answered the answer
Here's a link!
Public Class Form1
Private Sub configurationfile()
Dim reader As New System.Configuration.AppSettingsReader
Dim sourceFolder As String = reader.GetValue("Printer Name", GetType(String)).ToString().Replace(",", "")
If Not String.IsNullOrEmpty(sourceFolder) Then
TextBox1.Text = sourceFolder
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 + "\WindowsApplication3.exe"
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(fullpath)
config.AppSettings.Settings.Remove("Printer Name")
If CheckBox1.Checked Then
config.AppSettings.Settings.Add("Printer Name", TextBox1.Text)
Else
config.AppSettings.Settings.Add("Printer Name", "")
End If
'Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sb As New System.Text.StringBuilder
Using sprinters As New PrintDialog
sprinters.ShowNetwork = True
If sprinters.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim PrintSettings As PrinterSettings = sprinters.PrinterSettings
sb.Append(sprinters.PrinterSettings.PrinterName & vbNewLine)
TextBox1.Text = sb.ToString & vbNewLine
End If
End Using
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
configurationfile()
End Sub
End Class

Related

get file name without files extension using drag and drop vb.net

I want to drag and drop a file on a button and store the files Name without extension to a text box. Help to get out of this problem. I am getting some errors on those codes.
Private Sub Button5_DragDrop(sender As Object, e As DragEventArgs)
_Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim file_name As String = Path.GetFileName(files(0))
For Each path In files
TextBox1.Text = (path)
Next
TextBox2.Text = files(0)
End Sub
Private Sub Button5_DragEnter(sender As Object, e As DragEventArgs)
_Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
I feel you missed setting AllowDrop feature to true
and also there is a function to get filename without extension "GetFileNameWithoutExtension"
Check the code Below
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button5.AllowDrop = True
End Sub
Private Sub Button5_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each indpath In files
TextBox1.Text = Path.GetFileNameWithoutExtension(indpath) & vbNewLine & TextBox1.Text
Next
End Sub
Private Sub Button5_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
End Class

Using open file dialog and save file dialog with a list box in VB

I need to save things someone adds to a list and open a txt file putting it into a list box. When I open a txt file I only get one line of code and my attempts to save only produce empty txt files. Any help will be greatly appreciated. Here is my code:
Imports System.IO
Public Class Form1
Public Listed As String
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim AllText As String = "", LineOfText As String = ""
Dim StreamToDisplay As StreamReader
OpenFileDialog1.Filter = "Text files (*.txt)}|*.txt"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Try
StreamToDisplay = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
Label1.Text = OpenFileDialog1.FileName
Do Until StreamToDisplay.EndOfStream
LineOfText = StreamToDisplay.ReadLine()
'AllText = AllText & LineOfText & vbCrLf
lstBox.Items.Add(Listed)
Loop
lstBox.Items.Add(AllText).ToString()
StreamToDisplay.Close()
CloseToolStripMenuItem.Enabled = True
OpenToolStripMenuItem.Enabled = False
Catch ex As Exception
MsgBox("An error occurred." & vbCrLf & ex.Message)
End Try
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, lstBox.Items.ToString(), False)
End If
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
lstBox.Items.Clear()
Label1.Text = ""
CloseToolStripMenuItem.Enabled = False
OpenToolStripMenuItem.Enabled = True
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Prompt As String = "Enter Items To Add Here"
Listed = InputBox(Prompt)
lstBox.Items.Add(Listed).ToString()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
With lstBox
.Items.Remove(.SelectedItem)
End With
End Sub
End Class
Here's a simple example that:
adds items to a ListBox
saves them to a file
loads them from a file and populates the ListBox with them
Code:
Imports System.IO
Public Class Form1
Private Sub ButtonAddItem_Click(sender As Object, e As EventArgs) Handles ButtonAddItem.Click
ListBox1.Items.Add(DateTime.Now.Ticks)
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As Object In ListBox1.Items
writer.WriteLine(o)
Next
End Using
End If
End Sub
Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)
End If
End Sub
End Class

How do you read a file and display in listbox in VB.NET?

I was trying to display data from file in a list saved on the hard drive by clicking on a button, however I'm not sure on haw to do it properly:
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Dim TextLine As String
If System.IO.File.Exists(Filename) = True Then
Dim RecipeReader As New System.IO.StreamReader(Filename)
Do While RecipeReader.Peek() <> -1
TextLine = TextLine & RecipeReader.ReadLine() & vbNewLine
Loop
lstRecipes.Text = TextLine.Text
Else
MsgBox("File Does Not Exist")
End If
End Sub
I would be really grateful for assistance :D
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Try
lstRecipes.AddRange(File.ReadAllLines(FileName))
Catch
MsgBox("Unable to read file")
End Try
End Sub
Use:
lstRecipes.Items.Add(TextLine.Text)
More specifically before it jumps to the next item in the list, so this would go right after your assignment of TextLine.
you can do this:
Imports System
Imports System.IO
Public Class Form1
Public Shared listTXT, listOfTxt, listOfTxtFile As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\myDirectory\"
Dim parentinfo As New DirectoryInfo(path)
' Store Text file name in a list
For Each txtFile As FileSystemInfo In parentinfo.GetFileSystemInfos()
listTXT.Add(txtFile.Name)
Next
' Store Path of Text file in a list
For noOfTxtFile = 0 To listTXT.Count - 1
Dim pathOfFile As String = path & listTXT(noOfTxtFile)
listOfTxtFile.Add(pathOfFile)
Dim obj As System.IO.StreamReader
obj = System.IO.File.OpenText(pathOfFile)
While Not obj.EndOfStream
listOfTxt.Add(obj.ReadLine)
End While
Next
Dim lineOfTxt As Integer
Dim txt As String
For lineOfTxt = 0 To listOfTxt.Count - 1
txt = listOfTxt(lineOfTxt)
ListBox1.Items.Add(txt)
Next
End Sub
End Class

vb.net IO.StreamWriter only works in debug mode

It works perfectly in debug mode but it gives me 'The process cannot access the file because it is being used by another process' unhandled exception error when I run the built one and press the Save button(It loads but not saves after that).
Any advice, please.
Imports System.IO
Public Class DL1
Function DirExists(ByVal DirName As String) As Boolean
On Error GoTo ErrorHandler
DirExists = GetAttr(DirName) And vbDirectory
ErrorHandler:
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub LoadGlink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadGlink.Click
GlinkList.Items.Clear()
Dim fileReader As System.IO.StreamReader
fileReader = _
My.Computer.FileSystem.OpenTextFileReader("c:\Source\DL1\Glink.txt")
Dim mystring() As String = fileReader.ReadToEnd.Split(vbNewLine)
GlinkList.Items.AddRange(mystring)
fileReader.Close()
Me.Controls("Glinklist").Focus()
End Sub
Private Sub linktochrome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles linktochrome.Click
If GlinkList.SelectedItem IsNot Nothing Then
' selected item is sglink
Dim sglink = GlinkList.SelectedItem.ToString
Process.Start("C:\Users\1\AppData\Local\Google\Chrome\Application\chrome.exe", sglink)
End If
Me.Controls("Glinklist").Focus()
End Sub
Private Sub movetofol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movetofol.Click
Dim strDir As String
strDir = "C:\Users\1\Downloads\Glink\" & "\" & SFoltext.Text
If DirExists(Trim(strDir)) = False Then
MkDir(Trim(strDir))
End If
For Each f As String In Sresult1.Items
Dim f_name As String = Path.GetFileName(f)
My.Computer.FileSystem.MoveFile(f, strDir & "\" & f_name)
Next
Sresult1.Items.Clear()
GlinkList.Items.Remove(GlinkList.SelectedItem)
End Sub
Private Sub ChecKDL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChecKDL.Click
Sresult1.Items.Clear()
Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
fileList = My.Computer.FileSystem.GetFiles("C:\Users\1\Downloads\")
For Each foundFile As String In fileList
Sresult1.Items.Add(foundFile)
Next
End Sub
Private Sub Bsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bsave.Click
Dim i As Integer
'Saves Glinklist
Dim path As String = System.IO.Path.Combine("c:\Source\DL1\", "Glink.txt")
Using fs As New System.IO.FileStream(path, IO.FileMode.Create)
Using w As IO.StreamWriter = New IO.StreamWriter(fs)
For i = 0 To GlinkList.Items.Count - 1
w.WriteLine(GlinkList.Items.Item(i))
Next
w.Close()
End Using
End Using
End Sub
Private Sub RMselec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RMselec.Click
GlinkList.Items.Remove(GlinkList.SelectedItem)
Me.Controls("Glinklist").Focus()
End Sub
End Class
Not sure it this could be the answer, but I will post here because I can better format the code.
Let me know if this changes anything
Change your reading function to
Using fileReader = _
My.Computer.FileSystem.OpenTextFileReader("c:\Source\DL1\Glink.txt")
Dim mystring() = fileReader.ReadToEnd.Split(vbNewLine)
GlinkList.Items.AddRange(mystring)
End Using
Or simply
Dim mystring() = File.ReadAllLines()
GlinkList.Items.AddRange(mystring)
Usually this means a permission issue. Try running Visual Studio as admin to eliminate that possibility.

Move Files using Kill in Visual Basic

I am making a desktop cleaner and I want the program to search For files extensions and move them into a new folder each named after the extension name. Here is what I have.
Public Class Form2
Private Sub Form_Load()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
Me.FileReference.Text = MyFolderBrowser.SelectedPath
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Label1.Text = "Cleaned."
If CheckBox1.Checked = True Then
On Error Resume Next
Kill(Me.FileReference.Text("\*.txt"))
If Not Directory.Exists(FileReference.Text) Then
Directory.CreateDirectory(FileReference.Text)
End If
End If
End Sub
End Class
I want to use Kill(Me.FileReference.Text("\*.txt")) to move the files with .txt extention in the Directory which the textbox named Filereference.text contains which is extracted using MyFolderBrowser.SelectedPath.
How can I do this?
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
FileIO.SearchOption.SearchAllSubDirectories, "*.*")
Dim foundFileInfo As New System.IO.FileInfo(foundFile)
My.Computer.FileSystem.MoveFile(foundFile, "C:\StorageDir\" & foundFileInfo.Name)
Next