Error copying from resources - vb.net

While copying from resources to a folder under appdata folder: i get an error, but I'm not finding any mistake in code..
Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)
Dim HelpRTF = (MainPath & "\Help.rtf")
Helpbox.LoadFile(HelpRTF)
End Sub
HelpRTF is a .rtf file, MainPath is a directory under %appdata% folder
Error: Value of type 'String' cannot be converted to 'Byte()'.
Error at: My.Resources.HelpRTF

The reason why you get that error is because the second parameter of the File.WriteAllBytes() method takes a Byte(), not a String. If you want to write text (String) to a file, you can use the File.WriteAllText() method.
Since RTF's can contain images, text, etc. treating it as text can corrupt it and needless to say, encoding issues might occur. So, instead of using the File.WriteAllText() method, change the FileType of the HelpRTF resource to Binary instead of Text like this:
After that, you can use your code as it was:
Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)
Dim HelpRTF = (SWinPath & "\Help.rtf")
Helpbox.LoadFile(HelpRTF)
End Sub
References:
File.WriteAllText - MSDN
File.WriteAllBytes - MSDN

Related

Why Is A FilePath Item With ~$ Added To ListBox

I am adding file paths from a folder to a List Box which are then opened as text in a Rich Text Box. I have used the same syntax as the code below for achieving the same purpose in another List Box and it works just fine. But, in the current example, I have two files in the default MyProjects folder (i.e. default folder is created by my app), but when I add the file paths from the folder as items to the List Box, I get a third item with ~$ in the file path? This item is obviously some kind of repetition of the first file path in the list? The two files in the default folder are also created by my app so, if this is a file access issue, I don't understand why I wouldn't have access to a file created by my app? Can anyone give me a clue what's happening here?
What I have Tried:
I have tried debugging to check where the extra file path is coming from. As far as I can tell, it is being created when I add the file paths to the List Box? i.e. commenting out the code for adding the items to the List Box stops all items being added, but doesn't tell me where this extra item is coming from?
The "Extra Item" Issue:
System.Windows.Forms.ListBox+ObjectCollectionC:\Users\username\Documents\MySolution\MyProjects\RTFdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects\Testdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects~$Fdoc.rtf
The Code:
lbxName.Items.AddRange(Directory.GetFiles("C:\Users\" + username + "\Documents\MySolution\MyProjects"))
lbxName.SelectedIndex = 0
Code For Loading:
For Each item In lbxName.SelectedItems
RTB.LoadFile(lbxName.SelectedItem, RichTextBoxStreamType.RichText)
Next
I cannot reproduce the error in the following code.
Private Sub FillListBox()
ListBox1.Items.AddRange(Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects"))
ListBox1.SelectedIndex = 0
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillListBox()
End Sub
Please read the comments in the following code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
'The following will overwrite the contents of the RichTextBox on each iteration
'This overload of LoadFile will only handle .rtf files
RichTextBox1.LoadFile(item.ToString)
Next
End Sub
I suggest you set the SelectionMode property to One in the designer and do the following.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.LoadFile(ListBox1.SelectedItem.ToString)
End Sub
Try to use as follows:
Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects").Where(Function(f)
Return New IO.FileInfo(f).Attributes & IO.FileAttributes.Hidden & IO.FileAttributes.System = 0

"Character is not valid" VB.NET

So what this does is copy a report from the resources onto the computer and run it. The code is:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dir As String = My.Computer.FileSystem.SpecialDirectories.Temp
Dim filename As String = dir + +"Report.exe"
IO.File.WriteAllBytes(filename, My.Resources.Report)
Process.Start(filename)
End Sub
It gives me an error for Process.Start saying
"Character is not valid"
I made another form and wrote the Process.Start part and gave me no errors. I tried removing all the previous code and replacing "filename" in Process.Start with an actual directory but nothing helps. I really need to some help on this, thanks.
What is likely occurring is that the temp folder is returning an invalid character. Instead, try using the IO.Path.GetTempPath method and also build the path by using the IO.Path.Combine method. Here is an example of building the String:
Dim dir As String = IO.Path.GetTempPath
Dim filename As String = IO.Path.Combine(dir, "report.exe")

Equals Not Working VB.Net

I am trying to compare two strings that I know are equal to each other, but it is always skipping to the else. I've tried everything, .Equals, =, IsNot, they all don't work! The frustrating part is that I know the strings are equal! Please take a look at my code and see if it there is possible anything wrong with it.
Public Class Form1
Dim log As String
WithEvents xworker As New System.ComponentModel.BackgroundWorker
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
xworker.RunWorkerAsync()
End Sub
Private Sub xWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles xworker.DoWork
Dim qWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Dim client As New Net.WebClient
log = client.DownloadString("http://########/log.txt")
End Sub
Private Sub xWorker_Completed(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles xworker.RunWorkerCompleted
If log.Equals(RichTextBox1.Text) Then
xworker.RunWorkerAsync()
Else
RichTextBox1.Text = log
xworker.RunWorkerAsync()
End If
End Sub
End Class
You needed to listen to #SLaks and #Hans Passant, they were right on the money.
I setup your code sample and it worked correctly if the source log.txt file didn't have a line terminator in it. Once I added the line terminator I got the results your are getting.
From the command window:
>? RichTextBox1.Text.Length
14
>? log.length
15
Using the QuickWatch window, and TABing until the Value field was selected:
Log result:
"log test 1234" & vbCrLf & ""
RichTextBox result:
"log test 1234" & vbLf & ""
The fix to the problem depends on what will actually get written to the log.txt file. I assume that "log test 1234" is just development code. If you are only interested in a single line as a result code then make sure you are not writing a line terminator. If your result codes are more complicated then you will need to do more parsing on the result than just an Equals compare.
Try this instead.
If log.ToLower().Trim() = RichTextBox1.Text.ToLower().Trim() Then
I think this is case sensitve compare. You should convert both of the strings to upper or to lower and then compare them
If Log.ToLower() = RichTextBox1.Text.ToLower() Then
Or you can use String.Compare method and set third param to true to ignore case
If String.Compare(log, RichTextBox1.Text, True) = 0 Then
I've read that the RichTextBox can change line endings when Text gets set. So the Text property might be returning a string that is different than what was set. I haven't been able to verify, but you can probably devise a quick test of this theory.

How to save a txt file to a predetermined location in vb 2010?

Hi i have a textbox which displays a bunch of names on it. The names are within a string called "strNames". I'm trying to have a save button which saves the names as a txt file in a predetermined location. Here is the code for the save button. It creates the file but without the list of names. please help!
Given the fact that your strNames is an array of strings then you could use File.WriteAllLines, no need to use a StreamWriter here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
File.WriteAllLines("C:\Test.txt", strNames)
End Sub
This has an advantage against the StreamWriter approach if you don't need particular processing to your input array before writing it to file, no foreach around the array strings and also you don't need to encapsulate the StreamWriter in a Using statement to ensure a proper release of the system resources
You need to write to the file and then call Close() on the StreamWriter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim w As IO.StreamWriter
w = New IO.StreamWriter("C:\Test.txt")
' strNames is an array so you have to iterate through the array and write each element
For Each item As String In strNames
w.WriteLine(item)
Next
w.Close()
End Sub
Make sure to use Try/Catch blocks in case any exception occurs during accessing and writing to the file.
Also, when you use resources such as Files, make sure to properly dispose of the object that's connected to the resource. Using the 'Using' statement is a great and safe way to do that. Here's an example of both in action.
Sub WriteToFile()
Dim strNames As String() = {"John", "Jimmy", "Joe"}
Try
Using oWriter As New IO.StreamWriter("C:\testFile.txt")
For Each strName As String In strNames
oWriter.WriteLine(strName)
Next
End Using
Catch ex As Exception
HandleException(ex)
End Try
End Sub

The system cannot find the reference specified for an assembly

I have a VB.NET solution which have a few projects and a 'References' folder in the root directory.
Now I want to add a new dll reference to one of the projects from this References folder but getting the following message:
The system cannot find the reference specified
How can this be solved?
I encountered this many times. Here I was trying to execute a jar file when a picturebox was clicked. I added my jar to my resources :
Private Sub PictureBox9_Click(sender As Object, e As System.EventArgs) Handles _PictureBox9.Click
Dim dir As String = My.Computer.FileSystem.SpecialDirectories.Temp
Dim filename As String = dir + "minecraft.jar" 'Look Below
IO.File.WriteAllBytes(filename, My.Resources.minecraft)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "minecraft.jar")
End Sub
The following worked for other programs though, but not minecraft, :
Private Sub Panel9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel9.Click
proc = Process.Start("vbexpress.exe", "")
End Sub
*proc was a New System.Diagnostics.Process()
But for actual files, you have manually lure them out of their location.
So I did Me.Resources.1234.png which saved me writing the directory. But this still did not work with minecraft, so I kept that long paragraph about in place.