Get filename of current RDLC report - vb.net

I need to get the filename of the current RDLC being displayed on the ReportViewer. Is there something like
Dim filename As String = Me.ReportViewer1.LocalReport.GetFilename?
because I do not want to do the following.
Dim path As String() = Me.ReportViewer1.LocalReport.ReportPath.Split("\")
Dim filename As String = path(path.Length - 1)

There doesn't seem to be any members in the ReportViewer or its LocalReport property to enable you to do this.
However, you might be able use a method like Path.GetFileName to do something like:
Dim filename As String = Path.GetFileName(Me.ReportViewer1.LocalReport.ReportPath)
So you're not doing the string split operation etc.

I completely agree with NKVU's answer and additionally, If you want to get the filename without its extension, You can use this,
Path.GetFileNameWithoutExtension(Me.ReportViewer1.LocalReport.ReportPath)

Related

How to get most recent modified date pdf file and display on VB from?

i'm trying to get vb to read the most recent modified pdf file in a specific folder and display the pdf file on my vb form. I only able to create a simple pdf display on my vb form and i'm stuck at this. Can anyone help?
Unable to find solution to my problem.
Dim testFile As System.IO.FileInfo
Dim fileName As String
Dim folderPath As String
Dim fullPath As String
testFile = My.Computer.FileSystem.GetFileInfo("C:\Users\example.pdf")
folderPath = testFile.DirectoryName
fileName = testFile.Name
fullPath = My.Computer.FileSystem.CombinePath(folderPath, fileName)
AxAcroPDF1.src = fullPath
my vb form should display the PDF based on the most recent modified file.
You can use the IO.DirectoryInfo class to get every IO.FileInfo in a directory while specifically targeting PDF files, then use LINQ to order them by their LastWriteTime, and then get the last file from the collection:
Dim folder As IO.DirectoryInfo = New IO.DirectoryInfo("my folder path here")
Dim lastModifiedPdf As IO.FileInfo = folder.GetFiles("*.pdf").OrderBy(Function(f) f.LastWriteTime).LastOrDefault()
If lastModifiedPdf IsNot Nothing Then
'....
End If
You need to call two sets of functions to achieve this.
A. Directory.GetFiles - this will list all the files in a directory, and it has options to provide a search pattern and also look in sub-folders.
B. File.GetLastWriteTime - this will return the last modified time of the file you pass to it.
You can put these functions together like:
Private Function GetLatestModifiedFileName(searchFolder As String) As String
Dim retVal = "<empty>"
Dim filesInDirectory() = Directory.GetFiles(searchFolder)
Dim latestModifiedtime As DateTime = DateTime.MinValue
For Each fileInDirectory As String In filesInDirectory
Dim currentFileModifiedTime As DateTime = File.GetLastWriteTime(fileInDirectory)
If (currentFileModifiedTime > latestModifiedtime) Then
retVal = fileInDirectory
latestModifiedtime = currentFileModifiedTime
End If
Next
Debug.Print("File: '{0}' was last modified on: '{1}'", retVal, latestModifiedtime)
Return retVal
End Function
and ultimately call this function using:
Dim lastModifiedFileName = GetLatestModifiedFileName("D:\Documents\")
The variable lastModifiedFileName will contain the full path to the file that has the latest modified date/time.

Delete A File That Contains The App Name (VB.NET)

This is the code I'm Using:
Dim file As String
Dim prefetchPath As String
Dim FileName As String = My.Application.Info.AssemblyName
prefetchPath = Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine) & "\Prefetch"
For Each file In IO.Directory.GetFiles(prefetchPath)
If file.Contains(FileName) Then
IO.File.Delete(file)
End If
Next
i don't know why it does not work if i use FileName. But it work if i use this code
If file.Contains("Example.exe") Then
IO.File.Delete(file)
End If
I want to make sure that if someone changes the name of the application the code works the same way(I already running the file as Administrator)
Help me Thanks.
My guess is that AssemblyName only returns the name without the extension, try including the .exe. Also, it is worth noting that you can use the IO.DirectoryInfo class and pass the file name in the GetFiles method to cut out your For/Each loop.
Here is a quick example:
Dim prefetchPath As String = IO.Path.Combine(Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine), "Prefetch")
Dim FileName As String = My.Application.Info.AssemblyName & ".exe"
If New IO.DirectoryInfo(prefetchPath).GetFiles(FileName).Count > 0 Then
IO.File.Delete(IO.Path.Combine(prefetchPath, FileName))
End If

replace text multiple file vb.net

I want multiple files with a name and in a different format
I will replace some of the text
To do this, use the following command:
Dim sb As New StringBuilder(File.ReadAllText("tmp\boot_root\initrd\fstab.*"))
sb.Replace("ro,barrier", "ro,noatime,barrier")
sb.Replace("ro,errors", "ro,noatime,errors")
But this does not work. I need a better command to do this
help please
To get a list of all files matching a pattern in a given directory use Directory.Getfiles(path, pattern). Example:
Dim list as String()
Dim path as String = "C:\tmp\"
Dim filename as String = "fstab"
list = Directory.GetFiles(path, filename & ".*")
For Each file As String In list
'Logic goes here.
Next

vb check for specific file type in dir and perform code

I'm trying to make a program that checks for specific file type in a directory, then executes a code if there are any files of that type found.
I'm assuming something like this:
For Each foundFile As String In
My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
(If any found files are, for example, "txt" files, then display their content.)
Next
Thanks in advance.
You can use Directory.GetFiles or Directory.EnumerateFiles with a parameter for the extension-filter:
Dim directoryPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim allTxtFiles = Directory.EnumerateFiles(directoryPath, ".txt")
For each file As String In allTxtFiles
Console.WriteLine(file)
Next
The difference between both methods is that the first returns a String(), so loads all into memory immediately whereas the second returns a "query". If you want to use LINQ it's better to use EnumerateFiles, f.e. if you want to take the first 10 files:
Dim firstTenFiles As List(Of String) = allTxtFiles.Take(10).ToList()
Dim di As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
For Each fi In di.GetFiles("*.txt")
Dim content As String = My.Computer.FileSystem.ReadAllText(fi.FullName)
Console.WriteLine(fi.Name)
Next

vb.net How to save File as Word & Open Office Document?

I have a small programm and i want to save the File so i can read them later into when i open it.
How can i now save the File cause i must save 5 Variables and read them back into the Tool and if its possible i want to use the File in Word or OpenOffice too.
My Variables
Title - Pieces- SinglePrice- Totalprice
Please give me Examples for the Point in the right way.
Thanks everyone!
If all you want to do is store four variables from your program in a file that can also be read by Word and OpenOffice, you can do that easily enough with a text file. The following code assumes that Title is a String, Pieces is an Integer, SinglePrice and TotalPrice are Decimal.
Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim saveFile As String = System.IO.Path.Combine(folder, "Save.txt")
Dim vars() As String = {Title, Pieces.ToString, SinglePrice.ToString, TotalPrice.ToString}
System.IO.File.WriteAllLines(saveFile, vars)
If you need to read the file to restore the values of the variables, you can do it like this. Note that this code assumes that the file was written by the first snippet of code, otherwise it would be necessary to validate the contaents of the file before using it.
Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim saveFile As String = System.IO.Path.Combine(folder, "Save.txt")
Dim vars() As String = System.IO.File.ReadAllLines(saveFile)
Title = vars(0)
Pieces = CInt(vars(1))
SinglePrice = CDec(vars(2))
TotalPrice = CDec(vars(3))