Tuesday, March 20, 2012 2:25 PM
I would like to include a tutorial with my project. It will be located under the Help menu.
Problem is, that I don't know how to open a file because the user can install it wherever they want.
How can I include the word document file as part of the project so the code can open it no matter where it's installed?
-Nothing to see. Move along.
Tuesday, March 20, 2012 3:26 PM
Add the file as an embedded resource. Then when you want to open the file, copy it to a temporary location and open it. See How to: Add or Remove Resources. The only tricky part is knowing the internal "path" to the resource when retrieving it using Assembly.GetmanifestResourceStream.
- In the solution explorer, make a new folder in your project root called "Help". This will also create an actual folder on your file system.
- Put the Word document in the Help folder (in the file system). For the example I'm calling the document itself Help.docx.
- Right click on the Help folder in the solution explorer and select Add Existing Item. Select "All Files (*.*)" in the file type drop down and browse to the Help.docx document. The item will now appear in your solution explorer
- Click on the Word doc in solution explorer. In the Properties box, change "Build Action" to "Embedded Resource".
- The path of the embedded resource in the assembly will be probably be [AssemblyName].Help.docx. You can check for sure by calling Assembly.GetManifestResourceNames.
Here is some code to actually retrieve and use the resource:
Imports System.IO Imports System.Diagnostics Imports System.Reflection Public Module HelpResource Friend Sub ListMyResources() For Each rName As String In GetType(HelpResource).Assembly.GetManifestResourceNames() Debug.Print(rName) Next End Sub Friend Sub OpenHelpDoc() Dim thisAssembly As Assembly = GetType(HelpResource).Assembly Dim tempPath As String = Path.Combine(Path.GetTempPath, "Help.docx") Dim rsrcPath As String = thisAssembly.GetName().Name & ".Help.docx" Using docStream = thisAssembly.GetManifestResourceStream(rsrcPath) Dim bytes(docStream.Length) As Byte docStream.Read(bytes, 0, docStream.Length) Using outStream = New FileStream(tempPath, FileMode.Create) outStream.Write(bytes, 0, docStream.Length) End Using End Using Process.Start("WINWORD.exe", tempPath) End Sub End Module
Tuesday, March 20, 2012 9:57 PM
If the file is added as a resource, the application path can be retrieved by:
The resources will be = My.Application.Info.DirectoryPath & "\Resources\"
Thursday, March 22, 2012 1:09 PM
I get an error on this highlighted piece:
bytes(docStream.Length) As Byte
NullReferenceException was unhandled
Object reference not set to an instance of an object.
If I try to put "As New Byte" it says "Arrays cannot be declared with'New'."
-Nothing to see. Move along.
Thursday, March 22, 2012 1:40 PMYour resource probably has a different name. Run the ListMyResources sub to print the full names of the resources to the output window. What does it print?
Thursday, March 22, 2012 6:15 PM
ok, the rest of the story
Shell(My.Application.Info.DirectoryPath & "\Resources\" & myfilename, AppWinStyle.NormalFocus)
will use the default program to open the file in question. Recommend '*.htm' extension. You can link to other reference resource files that way.
Thursday, March 22, 2012 9:27 PM
Windows didn't pick up the default program as expected (getting old, have old ways)
This code will work, providing that there is a directory created named resources in the app directory, and that can be done when adding the specific file to the project compile. You can assign where the added file will reside with respect to the app\path when installed.
This code will work in Windows for any 'registered' file extension. *.htm will open in a web browser, *.rtf will open in Word, *.txt will open in NotePad.
All is well with the World Again.
If Dir(CStr(My.Application.Info.DirectoryPath) & "\resources\document.rtf"), <> "" Then
Shell("explorer.exe " & CStr(My.Application.Info.DirectoryPath) & "\resources\document.rtf", AppWinStyle.NormalFocus)
At least it works on Windows 7 64bit