Create folder in a specified location with strings - vb.net

I´ve been working in a code that allow user to create a folder in a location specified with folder browser and name it with two text strings, but at this far, I've managed to create the folder on the default location.
How can I set the location with the folder browser and have the new folder name with these texts as default.
The code this far is.
For create folder button:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim snombrecarpeta As String
Dim sruta As Object
snombrecarpeta = "QUO " & (quo.Text) & "_" & (proy.Text)
sruta = ccliente()
My.Computer.FileSystem.CreateDirectory(snombrecarpeta)
MsgBox("Se ha creado la carpeta del proyecto")
End Sub
For search folder button:
Private Sub ccliente_Click(sender As Object, e As EventArgs) Handles ccliente.Click
Dim ccliente = New FolderBrowserDialog()
ccliente.SelectedPath = ("E:\Crear_carpetas\Crear_carpetas")
If DialogResult.OK = ccliente.ShowDialog() Then
End If
Thanks in forward.
And yes, I´m just starting in the vb world.

Creation of folder on Desktop, see Environment.GetFolderPath, and IO.Directory.CreateDirectory
Dim snombrecarpeta As String
Dim sruta As Object
snombrecarpeta = "QUO " & (quo.Text) & "_" & (proy.Text)
sruta = ccliente()
'
' e.g. create directory on Desktop
'
snombrecarpeta = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), snombrecarpeta)
Try
IO.Directory.CreateDirectory(snombrecarpeta)
MessageBox.Show("Se ha creado la carpeta del proyecto")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
edit:
'
' https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog?view=net-5.0
'
Dim path As String = ""
Dim snombrecarpeta As String
snombrecarpeta = String.Format("QUO{0}_{1}", (quo.Text), (proy.Text))
Using fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.Desktop
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
path = IO.Path.Combine(fbd.SelectedPath, snombrecarpeta)
End If
End Using

Related

Find REGEX in every file in a folder

I need to evaluate each file in a folder for the ICN string. Then add each ICN to an output file. I found the code below and have made changes to it to meet my needs but it only adds one found file in the ICN.log instead of looping through all files.
Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click
Dim Regex = New Regex("[<][!]ENTITY (ICN.*?)[.]\w+")
Dim output = New List(Of String)
Dim tLoc = txtFolderPath.Text
Dim txtFiles = Directory.EnumerateFiles(tLoc, "*.xml", SearchOption.AllDirectories)
For Each tFile In txtFiles
Dim input = File.ReadAllText(tFile)
If Regex.IsMatch(input) Then
Console.Write("REGEX found in " + tFile)
output.Add(tFile)
Exit For
End If
Next
File.WriteAllLines(tLoc.TrimEnd("\"c) & "\ICN.log", output)
End Sub
After I removed the for exit for the code works.
Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click
Dim Regex = New Regex("[<][!]ENTITY (ICN.*?)[.]\w+")
Dim output = New List(Of String)
Dim tLoc = txtFolderPath.Text
Dim txtFiles = Directory.EnumerateFiles(tLoc, "*.xml", SearchOption.AllDirectories)
For Each tFile In txtFiles
'MsgBox(tFile)
Dim input = File.ReadAllText(tFile)
If Regex.IsMatch(input) Then
Console.Write("REGEX found in " + tFile)
output.Add(tFile)
'Exit For
End If
Next
File.WriteAllLines(tLoc.TrimEnd("\"c) & "\ICN.log", output)
MsgBox("Function Complete")
End Sub

Download a selected file from my GridView using DoubleClick

I am trying to download a file that I uploaded into my GridView. Each time I double click on the second document in the GridView, it opens the first uploaded document and saves it
Here is my code:
Private Sub dgDocuments_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles dgDocuments.DoubleClick
LoadUploadedDocuments()
Try
Dim objTransactionDocument As Objects.TransactionsDocuments
If dgDocuments.CurrentRowIndex.Equals(-1) Then
Dim obj As Object = dgDocuments.Item(dgDocuments.CurrentRowIndex, 1)
objTransactionDocument = Managers.TransactionsDocuments.SelectTransactionsDocuments(Convert.ToInt32(obj))
End If
If Not objTransactionDocument Is Nothing Then
If objTransactionDocument.TransactionsDocumentsID = 1 Then
Dim saveFile As New SaveFileDialog
saveFile.FileName = objTransactionDocument.FileLocation.Substring(objTransactionDocument.FileLocation.LastIndexOf("\"))
saveFile.Title = "Download supporting document to transaction " + m_objTransaction.TransactionID.ToString()
If saveFile.ShowDialog() = DialogResult.OK Then
Dim saveDir As String = Path.GetDirectoryName(saveFile.FileName)
Dim name As String = objTransactionDocument.FileLocation.Substring(objTransactionDocument.FileLocation.LastIndexOf("\"))
Dim saveLocation As String = saveDir + name
System.IO.File.Copy(objTransactionDocument.FileLocation, saveLocation)
MessageBox.Show("Document saved successfully")
End If
End If
End If
Catch ex As Exception
MessageBox.Show("Document saved unsuccessfully " + ex.ToString())
End Try
End Sub
What am I doing wrong?
add / to saveDir as
saveDir+='/'

Vb.net - FolderBrowserDialog

I am having some troubles with FolderBrowserDialog
I've tried all the post I could find here and I'm almost there in terms of what I want.
following is my code:
Private Sub ButtonBrowseOutput_Click(sender As Object, e As EventArgs) Handles ButtonBrowseOutput.Click
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = Application.StartupPath
If DialogResult.OK = dialog.ShowDialog() Then
TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
End If
End Sub
would give me something like this:
System.Windows.Forms.FolderBrowserDialog/helloforum.txt
Where I want it to give it for example:
c:/users/sexyname/desktop/helloforum.txt
TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
Must be:
TextBoxShowOutput.Text = dialog.SelectedPath & "/helloforum" & ".txt"
SelectedPath - Gets or sets the path selected by the user.
dialog.SelectedPath & "/helloforum.txt"
Just for your knowledge
Private Sub AbsolutePathOfDialogBoxes()
Dim dlgFolder = New FolderBrowserDialog
Dim dlgOpenFile = New OpenFileDialog
Dim dlgSaveFile = New SaveFileDialog
Dim absolutePath As String
'/*-----------------------------------*/'
absolutePath = dlgFolder.SelectedPath
absolutePath = dlgOpenFile.FileName
absolutePath = dlgSaveFile.FileName
'/*-----------------------------------*/'
End Sub
Happy Coding

How do I get the file name only, assign it to a variable and use later?

I have the following code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
Dim saveFileName As String = ""
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
saveFileDialog1.FileName = saveFileName
Using sw As StreamWriter = New StreamWriter(myStream)
' Add some text to the file.
sw.WriteLine(DateTime.Now + " - " + saveFileName) ' Date and File title header
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Using
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
End Sub
The variable is saveFileName, however when I use the saveFileDialog1.FileName as it, it is empty or provides the full path to the file, how do I only get the name? (such as test.txt)
Use Path.GetFileName
saveFileName = Path.GetFileName(saveFileDialog1.FileName)
DIM fileName = IO.Path.GetFileName(SavefileDialog.FileName)

Print Preview not displaying file

I am working on a menu strip Print Preview event. i worked out the following procedure to view a pdf file located in the same path as my workbook.
Private WithEvents docPrint As New PrintDocument()
' Declare a string to hold the entire document contents.
Private documentContents As String
' Declare a variable to hold the portion of the document that
' is not printed.
Private stringToPrint As String
Private Sub ReadDocument()
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim docName As String = xlWBPath & "\" & "CustomRanges.pdf"
docPrint.DocumentName = docName
Dim stream As New FileStream(docName, FileMode.Open)
Try
Dim reader As New StreamReader(stream)
Try
documentContents = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
stream.Dispose()
End Try
stringToPrint = documentContents
End Sub
Private Sub prnPrvStripCustomRange_Click(sender As Object, e As EventArgs) Handles prnPrvStripCustomRanges.Click
ReadDocument()
prnPrvCustomRanges.Document = docPrint
prnPrvCustomRanges.ShowDialog()
End Sub
The dialog pops up on click, but my document page is blank. I have another event for printing and that one prints the same document. So I don't understand what I am doing wrong on my preview.
Here is my print procedure:
Private Sub prnCustonPages_Click(sender As Object, e As EventArgs) Handles prnCustonPages.Click
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim docName As String = xlWBPath & "\" & "CustomRanges.pdf"
docPrint.DocumentName = docName
prnPrtCustomRanges.Document = docPrint
prnPrtCustomRanges.PrinterSettings = docPrint.PrinterSettings
prnPrtCustomRanges.AllowSomePages = True
If prnPrtCustomRanges.ShowDialog = DialogResult.OK Then
docPrint.PrinterSettings = prnPrtCustomRanges.PrinterSettings
docPrint.Print()
End If
End Sub