Access .exe file in resources? - vb.net

I have an encrypted program in my resources in a project I'm working on and I need to access that file like this.
Dim fs As New FileStream(filepath, FileMode.Open)
Any help?

try GetManifestResourceStream - it gives you a stream to read the resource by name. This stream can be used for example to decrypt and/or write to a real file and/or load as an Assembly (if the embedded resource is a .NET exe/DLL)...
For sample code see http://www.eggheadcafe.com/microsoft/VB-NET/33899321/how-to-extract-a-resource-to-a-file.aspx .

Related

Download dynamically generated content in VB.net

I am trying to download a dynamically generated CSV file over HTTP in VB.net. I have tried various download methods, but all generate the following error:
System.Net.WebException was unhandled. The request was aborted: The connection was closed unexpectedly.
The specific CSV file I am trying to fetch is http://www.ukrepeater.net/csvcreate.php
I can only assume the error is due to the fact that the data isn't located at the actual URL referenced, but is just a dynamically generated file that, in a regular browser, just pops-up a download window.
I truly have tried to find every similar question already asked, but I cannot find a solution anywhere. Any code suggestions or links to the answer would be gratefully received!
Try with System.Net.WebClient class. You can save the content of your csv file in a string variable:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://www.ukrepeater.net/csvcreate.php")
or you can download the file to your file system:
Dim webClient As New System.Net.WebClient
webClient.DownloadFile("http://www.ukrepeater.net/csvcreate.php", "c:\file.csv")
More info about System.Net.WebClient can be found on this MSDN page

Reading Xml from an absolute path

I need to access a remote Xml document from a WCF service. Right now I have:
XmlReader reader = XmlReader.Create("path");
But since the Xml doc is elsewhere on our network I need to give the XmlReader an absolute path, as opposed to having it look deeper in the project folder. How do I do this? I've found surprisingly little information about this. It seems like this should be a simple thing to do. Any help is appreciated!
Thanks
You can use overload that accepts Stream parameters as follows:
using (FileStream fileStream = new FileStream(#"\\computername\shared path"))
using (XmlReader reader = XmlReader.Create(fileStream))
{
// perform your custom code with XmlReader
}
Please note that you need appropriate permission to open remote stream. In WCF service context you may need to use impersonation.

.NET ZipPackage vs DotNetZip when getting streams to entries

I have been using the ZipPackage-class in .NET for some time and I really like the simple and intuitive API it has. When reading from an entry I do entry.GetStream() and I read from this stream. When writing/updating an entry I do entry.GetStream(FileAccess.ReadWrite) and write to this stream. Very simple and useful because I can hand over the reading/writing to some other code not knowing where the Stream comes from originally.
Now since the ZipPackage-API doesn't contain support for entry properties such as LastModified etc I have been looking into other zip-api's such as DotNetZip. But I'm a bit confused over how to use it. For instance, when wanting to read from an entry I first have to extract the entire entry into a MemoryStream, seek to the beginning and hand-over this stream to my other code. And to write to an entry I have to input a stream that the ZipEntry itself can read from. This seem very backwards to me. Am I using this API in a wrong way?
Isn't it possible for the ZipEntry to deliver the file straight from the disk where it is stored and extract it as the reader reads it? Does it really need to be fully extracted into memory first? I'm no expert but it seems wrong to me.
using the DotNetZip libraries does not require you to read the entire zip file into a memory stream. When you instantiate an instance an instance of ZipFile as shown below, the library is only reading from the zip file header. The zip file headers contain properties such as last modified, etc. Here is an example of opening a zip file. The DotNetZip library then reads the zip file headers and constructs a list of all entries on the zip:
using (Ionic.Zip.ZipFile zipFile = Ionic.Zip.ZipFile.Read(this.FileAbsolutePath))
{
...
}
It's up to you to then extract zip files either to a stream, to the file system, etc. In the example below, I'm using a string property accessor on zipFile to get a zip file named SomeFile.txt. The matching ZipEntry object is then extracted to a memory stream.
MemoryStream memStr = new MemoryStream();
zipFile["SomeFile.txt"].Extract(memStr); // Response.OutputStream);
Zip entries must be read into the .NET process space in order to be deflated, there's no way to bypass that by going straight into the filesystem. Similar to how your Windows Explorer shell zip extractor would work - The Windows shell extensions for 7zip or Windows built in Compressed Folders have to read entries into memory and then write them to the file system in order for you to be able to open an entry.
Okey I'm answering this my self because I found the answers. There are apparently methods for both these things I wanted in DotNetZip. For opening a read-stream -> myZipEntry.OpenReader() and for opening a write-stream -> myZipFile.UpdateEntry(e, (fn, obj) => Serialize(obj)). This works fine.

VB.Net How to move the app.config file to a custom location

I have an application that has a load of values in its app.exe.config file. The application is used by a few users, and the settings would change on a regular basis. so im having to change the config file, and send it out to all users.
I'd love to move the config file to the network somewhere and point the app to this file. ive tried to use;
Imports System.Configuration.ConfigurationManager
OpenExeConfiguration("I:\app config\HelpDeskQuickCallLogger.exe.config")
But i cant get it to read in the values.
Anyone any ideas?
This is how we handle this requirement if a specific configuration file (sSpecificConfigurationFile) is specified:
Dim oConfig As System.Configuration.Configuration
If sSpecificConfigurationFile.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase) Then
Dim oMap As New ExeConfigurationFileMap
oMap.ExeConfigFilename = sSpecificConfigurationFile
oConfig = ConfigurationManager.OpenMappedExeConfiguration(oMap, ConfigurationUserLevel.None)
Else
oConfig = ConfigurationManager.OpenExeConfiguration(sSpecificConfigurationFile)
End If
I am not sure if this is what you are looking for but see if this Code Project Article helps.
Description from above article:
This article demonstrates how to write a custom Settings Provider to
allow you to persist your My.Settings to your own storage system.

Embed Dictionary into VB.NET application

I want to embed a dictionary.txt which my program uses a streamreader object to parse. I tried to add it to resources but then the streamreader had an error. How can it be properly done?
Thanks
First you need to embed the file in your assembly (add to project and goto Properties for the file, and set Build Action to "Embedded Resource").
Then you need to access and read it's contents using GetManifestResourceStream():
Getting an embedded resource file out of an assembly
This article might be of interest: Microsoft .NET Framework Resource Basics