How to extract embedded resource from a namespace with special characters? - vb.net

I'm trying to extract an embedded resource to VB.Net project (build action : Embedded resource) using the following command :
Dim StreamReader As StreamReader
StreamReader = New StreamReader(Assembly.GetManifestResourceStream(NameSpaceName & "." & FileName))
Where Assembly is the executing assembly.
The file name is "file.sql" the namespacename is like "A.B.C" but the result of GetManifestResourceStream is nothing
Does anyone know how to use the a namespace name with some special charachters on it please ?
Thanks

You can use Assembly.GetManifestResourceNames method to returns the names of all the resources in the assembly.

Related

How can i store characters like /xfe\xff\x8bE\xfc\x8b\xdf\x8b}\ in a variable -Vb.net

I heard that you can't merge to .exe files to gether so I thought what will happen if i stored the executable app as binary inside my app and then built it.
In python I did it:
myappBinary =
with open("sample.exe", 'wb') as outf:
outf.write(b'''3\xb4\xcb^\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x''') # for example
but in Vb.net I can't even store the binary data, any help?
I tried:
Dim content() As Byte = <![CDATA[3\xb4\xcb^\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x]]>.Value
And I tried to call the cmd to run echo BINARYDATA > sample.exe and it's not working
Then I tried:
Dim content As Byte = "3\xb4\xcb^\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x"
every time it shows me this msg Value of type 'String' cannot be converted to 'Byte()' or something realted to it
THE PROBLEM WILL SOLVED IF I CAN STORE A BINARIES DATA AND WRITE THEM.
I know that i can simply read an .exe file using Dim data2() As Byte = File.ReadAllBytes("myApp.exe") but i want to store the data in a variable.
Any help?

VB.net rename each file in a directory

Id like to rename all my files in one specific directory. They shall all get the identical extension. I tried using a for loop:
For Each s As String In IO.Directory.GetFiles(Environ("PROGRAMFILES(x86)"), "*", IO.SearchOption.AllDirectories)
Try
My.Computer.FileSystem.RenameFile(s, s & ".new")
Catch ex As Exception
End Try
So that the name s (as string) becomes s & extension ".new"
However, that didnt work.
If you had read the documentation for the RenameFile method you are calling, as you should have to begin with but especially when it didn't work, then you would know that the first argument requires the full path of the file while the second argument requires just the new name of the file. That means that you need this:
My.Computer.FileSystem.RenameFile(s, My.Computer.FileSystem.GetName(s) & ".new")
The File.Move method requires full paths in both cases because it supports renaming within the same folder and moving to a different folder. You say that you want to use RenameFile but didn't bother to note how it is different, i.e. it only supports renaming within the same folder so specifying that path twice is pointless and allowing different paths to be specified would cause problems.

How to convert DM-script files (*.s) into a plugin file (*.gtk)

I have written some DM-script files (.s), and thus I would like to convert (compile?) them into plugin files (.gtk). In this case, should I prepare some kind of compiler and Gatan Software Development Kit (SDK) package, or others? If there is a good way, please teach the procedure in detail. I will be really grateful, if you share some wisdom to create *.gtk files.
.GTK files are not compiled - you will not gain any speed benefit of having them in a .gtk (or .gt1, .gt2 ) file. They are only packed into those files for convenience of sharing and distributing them easily without sharing source code.
That said, the way to "pack" scripts into those files is to use a script-command itself, i.e. write a script which build these files from a set of .s files on the hard drive.
The command to add a script-file to a script package is AddScriptFileToPackage with the syntax:
void AddScriptFileToPackage( String file_path, String packageName, Number packageLevel, String packageLocation, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
void AddScriptFileToPackage( String file_path, String packageName, Number packageLevel, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
The command to add a script (string) to a script package is AddScriptToPackage with the syntax:
void AddScriptToPackage( String script, String packageName, Number packageLevel, String packageLocation, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
void AddScriptToPackage( String script, String packageName, Number packageLevel, String command_name, String menu_name, String sub_menu_name, Boolean isLibrary )
The paramters in the two commands are:
Note, that the created plugin-file will by default appear in the user_plugin location:
C:\Users\USERNAME\AppData\Local\Gatan\Plugins
The second syntax allows specifying the path, where the packageLocation parameter might be any of the names also accepted in the command GetApplicationDirectory, most often either being user_plugin (see above) or just plugin referring to the current run DigitalMicrograph.exe relative plugin folder, i.e.
.\Plugins relative to where the DigitalMicrograph.exe sits, typically
C:\Program Files\Gatan\Plugins\
Note, that one can append scripts to an existing file, but can not "unistall" one from it. In that case, one has to delete the file and re-create it anew.
Also, if an error occurs in the to-be-added scripts, they will not install correctly and you might need to start over.
The F1 help documentation (of later GMS versions) has an example script showing how to typically use the command:
The difference between libary and command install is the same as when using the File-Menu command to "install a script". A library usually is a set of methods (or classes) which, when installed, remain available and in memory. The command installs a script as a menu-command to the UI. i.e. selecting the menu executes the scirpt (once).
While the "File/Install Script" install things into the general prefereces-file of DigitalMicrograph, the commands above create separate .gtk files which load on startup if found in the plugins-folder. Any code that is installed as "library" is run once on startup.

How to modify .INI file with using sharpconfig?

I am using sharpconfig for Load my .INI file. i got success in reading ini file and its very user friendly.
Dim config As New SharpConfig.Configuration
config = SharpConfig.Configuration.Load("D:\Myini.ini")
Now i want to replace particular word of .ini file. SharpConfig showing that its also providing the .INI file modify functionality but i can not able to find how i can modify my file with sharpconfig
Please help me. Thanks!
Given an INI file that looks like this:
[MySection]
MySetting = 123
You read it with SharpConfig.Configuration.Load:
Dim yourpath = "c:\WhatEver.ini"
Dim config = SharpConfig.Configuration.Load(yourpath)
Console.WriteLine(config("MySection")("MySetting").Value)
This code will print
123
Now, to change the INI file, simply assign a new value and save it like:
config("MySection")("MySetting").Value = "Foobar"
config.Save(yourpath)
The INI file will now look like this:
[MySection]
MySetting = Foobar
You already refered to the codeplex page and there you can even find a a test app that shows how to do stuff with it: TestApp
If you have the Category and the Setting you can get/set the value of the Setting with .Value
In the Sourcecode of SharpConfig it is easy readable what can be accessed. I'm a VB guy myself but it should be no problem to read the c# stuff >> Settings

Unzipping a File in vb.net

I have an embedded resource in my .exe that is a zip file, I would like to move it out of the resources and unzip it to a specific folder.
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btn_Install.Click
Dim Dir_File As String = "C:\FTB"
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)
End Sub
But I have no way of extracting the the .zip file to a directory. So all I need now is a way to actual extract the .zip.
I tried this:
Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)
For Each fileToDecompress As FileInfo In directorySelected.GetFiles("Unleashed.zip")
Using OriginalFileStream As FileStream = fileToDecompress.OpenRead()
Using decompressedFileStream As FileStream = File.Create(Dir_File & "\Unleashed")
Using decompressionStream As GZipStream = New GZipStream(OriginalFileStream, CompressionMode.Decompress)
decompressionStream.CopyTo(decompressedFileStream)
End Using
End Using
End Using
Next
But all that did was give me an error about a magic number.
Any help is very much appreciated.
Your problem is allready discussed here: https://stackoverflow.com/a/11273427/2655508
The GZipStream class is not suitable to read compressed archives in the .gz or .zip format. It only knows how to de/compress data, it doesn't know anything about the archive file structure. Which can contain multiple files, note how the class doesn't have any way to select the specific file in the archive you want to decompress.
The solution is to use NET 4.5 which contains inside the System.IO.Compression namespace the needed classes with their methods for creating, manipulating and extracting items in/out of a zip file.
See e.g. System.IO.Compression.ZipFileExtensions.ExtractToDirectory()
Gzip is different from zip. The file format is identified by the 2 to 4 first bytes of the file:
Zip: 50 4b 03 04
Gzip: 1f 8b
Since you are trying to decompress a zip file with gzip you get an error message essencially telling you that your file is not a gzip file.
See
Unzip files programmatically in .net for similar question.
For magic number see: http://en.m.wikipedia.org/wiki/Magic_number_(programming)
If you can use the NET 4.5 it ships inside the System.IO.Compression namespace and all the stuff needed for zip file manipulation.
For zip manipulation you can consider using a third party library like sharpziplib I use it with success in many projects.
Take a look to these samples: https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples