How to open file located inside winform project - vb.net

I have a project that requires some pdf reports to be opened from my forms. Since the program can be installed in any drive, I used the winform location directory as my path. I reports are located on a directory called Reports. It works well under development becuase I the winform is under bin\debug, but when I deploy my solution and try the procedure, it does not work, the file cannot be found. I created a Reports directory after deployment, but still did not find my pdf.
Here is the code:
'Open the requested file name. The files are stored in the
'same path as the main workbook.
'#param filename file to be opened
Function OpenReports(strFileName As String) As String
Dim reportFolder As String
Try
reportFolder = Path.Combine(Directory.GetCurrentDirectory(), "Reports")
System.Diagnostics.Process.Start(reportFolder & "\" & strFileName)
Catch ex As Exception
MsgBox("The " & strFileName & " is not found on directory")
End Try
Return strFileName
End Function
Private Sub btnRptRangesToMarket_Click(sender As Object, e As EventArgs) Handles btnRptRangesToMarket.Click
'This procedure runs when the btnRptRangesToMarket is clicked
'the procedure opens the pdf below by running the OpenReports Function
OpenReports("Ranges to Market.pdf")
End Sub

Try Application.StartupPath instead of Directory.GetCurrentDirectory(). I always get good results with that property.
This is from MSDN:
The current directory is distinct from the original directory, which is the one from which the process was started.

Related

visualbasic cant delete .zip file in a directory

i have three folder in "D:\TestField". These are "backups","ExampleFileUpdatePrimary","ExampleFileUpdateSecondary.zip". I can delete "D:\TestField\ExampleFileUpdatePrimary" folder ,but i cant delete "D:\TestField\ExampleFileUpdateSecondary.zip"
my sub function is ;
Private Sub DeleteFolder(ByVal path As String)
path = path.Remove(path.Length - 1) 'D:\TestField\ExampleFileUpdatePrimary
Dim path2 = path & ".zip" 'D:\TestField\ExampleFileUpdateSecondary.zip
My.Computer.FileSystem.DeleteDirectory(path, FileIO.DeleteDirectoryOption.DeleteAllContents) 'its working
'Dim di = New IO.DirectoryInfo(path2) 'try1
'di.Delete(True)
'IO.Directory.Delete(path2) 'try2
'My.Computer.FileSystem.DeleteDirectory(path2, FileIO.DeleteDirectoryOption.DeleteAllContents) 'try3
End Sub
try1,try2 exceptions says " The directory name is invalid"
try3 exception says "System.IO.DirectoryNotFoundException: ''D:\TestField\ExampleFileUpdateSecondary.zip "
i couldnot find a new solution or try to delete a .zip file in code
thanks
Directory entries ending on .zip are usually files not folders, even though Windows Explorer displays them as folders.
Since they are files, use File.Delete instead.

Saving image from php id URL using webbrowser in VB.net

I am trying to save a image from webbrowser but can't find an answer for it.
Example of the problem is the URL is hxxp://domain[.]com/image/?img=1234 on webbrowser it shows the image and the source is <img src='hxxp://domain[.]com/image/?img=1234'>..
I was unable to save it using the My.Computer.Network.DownloadFile and MemoryStream(tClient.DownloadData methods.
In order to download the file, I will need to session cookie also?
How can I do this?
Any image that you see in the WebBrowser has already been downloaded to your computer. It is normally cached in the folder:
C:\Users\<username>\AppData\Local\Microsoft\Windows\Temporary Internet Files
or whatever other folder is set in Internet Options. You can get your image from that folder.
The following program shows how to copy file TDAssurance_Col.png from the Temporary Internet Files folder to the C:\Temp folder.
Module Module1
Sub Main()
CopyFileFromTemporaryInternetFolder("TDAssurance_Col.png", "C:\Temp")
End Sub
Public Sub CopyFileFromTemporaryInternetFolder(filename As String, destinationFolder As String)
' Search actual path of filename.
Dim temporaryInternetFilesFolder As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "Content.IE5")
Dim pattern As String = Path.GetFileNameWithoutExtension(filename) & "*" & Path.GetExtension(filename)
Dim pathnames() As String = Directory.GetFiles(temporaryInternetFilesFolder, pattern, SearchOption.AllDirectories)
' If file found, copy it.
If pathnames.Count > 0 Then
File.Copy(pathnames(0), Path.Combine(destinationFolder, filename))
End If
End Sub
End Module

Open a folder using Process.Start for specific folder

Hi I searched the web and found the example Open a folder using Process.Start to open the folder. I followed it and created a console app but the folder is opened my document folder instead of my input folder. For my testing, I executed it in cmd prompt by typing "\sharefuser01\users$\tester\2012". It opened the "\sharefuser01\users$" folder. Would someone tell me how to do it. Thanks in advance.
There is my code:
Sub Main(ByVal args() As String)
Dim psi As New ProcessStartInfo(args(0))
Process.Start("explorer.exe", args(0))
End Sub
Process.Start("directory path")
or
Dim Proc As String = "Explorer.exe"
Dim Args As String =
ControlChars.Quote &
IO.Path.Combine("C:\", "Folder with spaces in the name") &
ControlChars.Quote
Process.Start(Proc, Args)

How to Access a txt file in a Folder created inside a VB project

I'm creating a VB project for Quiz App (in VS 2013). So I have some preset questions which are inside the project (I have created a folder inside my project and added a text file).
My question is how can I read and write contents to that file? Or if not is there any way to copy that txt file to Documents/MyAppname when installing the app so that I can edit it from that location?
In the example below I am focusing on accessing files one folder under the executable folder, not in another folder else wheres. Files are read if they exists and then depending on the first character on each line upper or lower case the line then save data back to the same file. Of course there are many ways to work with files, this is but one.
The following, created in the project folder in Solution Explorer a folder named Files, add to text files, textfile1.txt and textfile2.txt. Place several non empty lines in each with each line starting with a character. Each textfile, set in properties under solution explorer Copy to Output Directory to "Copy if newer".
Hopefully this is in tune with what you want. It may or may not work as expected via ClickOnce as I don't use ClickOnce to validate this.
In a form, one button with the following code.
Public Class Form1
Private TextFilePath As String =
IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "Files")
Private TextFiles As New List(Of String) From
{
"TextFile1.txt",
"TextFile2.txt",
"TextFile3.txt"
}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileName As String = ""
' loop thru each file
For Each fileBaseName As String In TextFiles
FileName = IO.Path.Combine(TextFilePath, fileBaseName)
' only access file if it exist currently
If IO.File.Exists(FileName) Then
' read file into string array
Dim contents As String() = IO.File.ReadAllLines(FileName)
' upper or lower case line based on first char.
' this means you can flip flop on each click on the button
For x As Integer = 0 To contents.Count - 1
If Char.IsUpper(CChar(contents(x))) Then
contents(x) = contents(x).ToLower
Else
contents(x) = contents(x).ToUpper
End If
Next
' save changes, being pesstimistic so we use a try-catch
Try
IO.File.WriteAllLines(FileName, contents)
Catch ex As Exception
Console.WriteLine("Attempted to save {0} failed. Error: {1}",
FileName,
ex.Message)
End Try
Else
Console.WriteLine("Does not exists {0}", FileName)
End If
Next
End Sub
End Class
This may help you
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Boot.ini")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Console.ReadLine()
You can also check this link.

Copy and Rename File VB Script Not Working For SSIS Script Task

I am dynamically creating reports using an Excel Template for an SSIS Package. I am attempting to copy the Excel template and rename it using VB 2010 in Script Task object.
The following is my code:
Public Sub Main()
Dim sourcePath As String = "\\server\Dir1\Dir2\Dir3\FileName_TEMPLATE.xlsx"
Dim destPath As String = "\\server\Dir1\Dir2\Dir3\FileName" + CDate(Date.Today.Date).ToString("yyyyMMdd") + ".xlsx"
If File.Exists(destPath) = True Then
File.Delete(destPath) 'delete existing file'
File.Copy(sourcePath, destPath) 'copy template file and rename'
End If
Dts.TaskResult = ScriptResults.Success
End Sub
End Class
I changed If File.Exists(destPath) = True Then... to If File.Exists(sourcePath) = True... to see if the sourcePath existed and then added a MessageBox("File doesn't exist") in an ELSE statement to so if even the source file exists and it is returning the MessageBox stating
"File doesn't exist"
The Template file is there and I copied and pasted the address from Windows Explorer window to the sourcePath string to ensure path accuracy.
The sourcePath is on a different server.
The file is in the source path.
What am I doing wrong?
Thanks