GetFullPath no output ?! VB.NET - vb.net

Good evening to all of you!
I have a problem that I can't solve with the method GetFullPath in vb.net.
What I want to do :
I would like to get the full path of a text file (test.txt) which is located in the same folder as the app.exe, the one I am working on. I need the full path to use an other method. To know more about the reason it doesn't work, I put the GetFullPath output in a MsgBox.
What is happening :
The MsgBox just shows a blank. This is really strange because, even if the test.txt doesn't exist, the output should exist (as if the file exists).
WARNING : IN MY CASE THE FILE EXISTS
Documentation : https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.100).aspx Cf. "Remarks"
What I think about that :
Is it possible that the app.exe doesn't see test.txt which is in the same folder ? I don't think so, it would give an outpout.
Maybe it is a problem of permission ? I really don't know why the MsgBox is empty.
My peace of code :
Dim file1 As String = "test.txt"
MsgBox(GetFullPath(file1))
Thankyou to everyone who will try to help me.
Have a nice day ! :)
Please excuse my poor english.
Microsoft Visual Studio 2010.

It appears something in your code is changing the working directory (or in the case of Windows XP, it is not being set properly). The GetFullPath function I have provided below will return the path I believe you are expecting regardless of the working directory.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(GetFullPath("file.txt"))
End Sub
Private Function GetFullPath(fileName As String) As String
Return IO.Path.Combine(Application.StartupPath, fileName)
End Function

Or you can do this ,
Label1.Text = Application.StartupPath
You do all with this code ^^

Related

Cannot save a freshly created bmp file, help needed

Hate to sound like a broken record, but I simply cannot get the program I wrote in Visual Basic to write a file when run standalone; but it works fine when run via the debugger.
Simplified, the code looks like this (and the bitmap image is being created and is being saved, i.e., the file does not already exist):
Using bmp As New Bitmap(imgHorSize, imgVerSize) ‘ the sizes are not too large
<build the bitmap>
Dim fName As String = “C:\Users\<name>\Desktop\temp\foobar.jpg"
bmp.Save(fName, ImageFormat.Jpeg)
End Using
When I run this outside the debugger, I get
System.UnauthorizedAccessException: Access to the path
'C:\Users<name>\Desktop\temp\foobar.jpg' is denied
I saw some notes that suggest there should be a \\ after C:. Other sources show using a / instead of a \. Not sure that either really matters (since the directory name is coming from the result of a FolderBrowserDialog). I have checked the folder permissions on temp and see that my account has full control. I added user Everyone, via the security tab, and gave it full control. Same problem. As an experiment, I changed the above code to
Using bmp As New Bitmap(imgHorSize, imgVerSize)
<build the bitmap>
Dim fName As String = “C:\Users\<name>\Desktop\temp\foobar.jpg"
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(fName, True)
file.WriteLine("Here is the first string.")
End Using
and I got the exact same error. I also tried creating a MemoryStream, but that also didn’t work.
So, the issue is probably not related to the bmp.Save process, but something more fundamental. I have been looking at various things and have tried a few of them, but nothing works. This should be super easy to do, so what am I doing wrong?
== follow up ==
To come up with something to share in its entirety, I just wrote the following, very simple program that basically does the same as the real program:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class Form1
Structure myImages
Dim strFName As String
Dim imgL As Image
End Structure
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim imgLI As myImages
imgLI.strFName = "C:\Users\<username>\Desktop\EinRiddle\ItemImages\An_Cat.jpg"
imgLI.imgL = New Bitmap(Image.FromFile(imgLI.strFName), 150, 150)
Dim bmp As New Bitmap(300, 400)
Using bmp
Dim gr As Graphics = Graphics.FromImage(bmp)
gr.Clear(Color.WhiteSmoke)
gr.DrawImage(imgLI.imgL, 20, 20)
bmp.Save("C:\Users\<username>\Desktop\testImg.jpg", ImageFormat.Jpeg)
gr.Dispose()
End Using
Application.Exit()
End
End Sub
End Class
I am getting the same error message...
Have you tried another directory? Often one cannot write a file to the desktop.
Have you tried another format (for example ImageFormat.Bmp)?
You use “ instead of " ... Is that the mistake? Ah, I guess this is not the real code and you wrote this from memory and and something made “
Sometimes it takes a while before the picture and graphics can be disposed because the image is saved lazily. Even if this is not welcomed, give it a try
bmp.Save("C:\Users\<username>\Desktop\testImg.jpg", ImageFormat.Jpeg)
Application.DoEvents()
gr.Dispose()
End Using
I found the answer: On this computer, my user files are on the D: drive. Thus, when I changed the path from C: to D:, it worked. Of course, why this is necessary, since there is symbolic link, I don't understand...

Extract file using SevenZip

I'm trying to add a file unzipper to my application, so I googled a little and stumbled on the sevenzipsharp library that is able to extract the most common archive formats.
So I for testing I created a simple application with a windows form.
So the entered data is the file location C:\Users\jeee\Desktop\CriticalSubPrintout.rar and the extract location C:\Users\jeee\Desktop\Test Extract
I added some code, without any documentation.. not my strong side apparently..
Imports SevenZip
Public Class Archiver
Private Sub btnExtractArchive_Click(sender As Object, e As EventArgs) Handles btnExtractArchive.Click
Dim Extractor As New SevenZipExtractor(tbExtractFile.Text)
Extractor.ExtractArchive(tbExtractPath.Text)
End Sub
End Class
This causes an error when I try and run the code
Can anyone provide a sample code, or a link to a good example how-to-use SevenZipSharp? Because I searched and can't find any VB.NET samples.
Or maybe just help me figure out what I need to do.
Thanks.
You need to call SevenZipBase.SetLibraryPath with the path to 7z.dll, and make sure that you are using the correct version for your application (32- or 64-bit). e.g.
SevenZipBase.SetLibraryPath("C:\Dev\7z.dll")
Dim Extractor As New SevenZipExtractor(tbExtractFile.Text)
Extractor.ExtractArchive(tbExtractPath.Text)

IO.File.Move doesn't work for files (VB.NET)

I am making a Program where you add files to an ListBox, and when you click the Install-Button it should move the files that got added to the ListBox, to an other folder. But this doesn't work, and i don't know how to fix. Here is my current Code:
Private Sub MoveFileBackgroundWorker(sender As Object, e As DoWorkEventArgs) Handles mfb.DoWork
For Each i As String In ListBox1.Items 'For each Items in ListBox1.Items, move.
IO.File.Move(i, mcpath_ & "\mods\")
Next
End Sub
Any help is really appreciated! PS: Sorry if my English isn't good, i am German :)
Move works in the following way:
IO.File.Move("C:\\myfile1.txt", "D:\\myfile2.txt")
It takes the source file as the first argument, and the target file as the second argument. In other words, the line above will move the file C:\myfile1.txt to a file named D:\myfile2.txt
So your line should be
IO.File.Move(i, IO.Path.Combine(mcpath_, "mods", IO.Path.GetFileName(i))
Which means: move file stored in i to the folder mcpath & "\mods\" with the same file name and extension
That's how i fixed it:
IO.File.Move(i, IO.Path.Combine(mcpath_.Trim, "mods", IO.Path.GetFileName(i)))
You need to add at mcpath_ .Trim!

How do I use My.Computer.FileSystem.CurrentDirectory with My.Computer.Network.DownloadFile?

I am trying to use it like this:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Computer.Network.DownloadFile(
"http://magicalexample.com/multi.zip", "My.Computer.FileSystem.CurrentDirectory\multi.zip")
Process.Start("cmd", "/k jar xf multi.zip")
MsgBox("Done.")
End Sub
End Class
(note: I screwed up the formatting, it's right in VB, don't worry lol)
When I try to use the update button, it tells me that destinationFileName needs to include a file name. Do they not play well together or what?
My.Computer.FileSystem.CourrentDirectory is a variable and isn't going to be magically parsed into your string. You need to concatenate the variable with the file location:
My.Computer.Network.DownloadFile("http://magicalexample.com/multi.zip", My.Computer.FileSystem.CurrentDirectory & "\multi.zip")
Or use the Path.Combine() method to merge the directory plus filename:
My.Computer.Network.DownloadFile("http://magicalexample.com/multi.zip", Path.Combine(My.Computer.FileSystem.CurrentDirectory, "multi.zip"))
Note that your examples aren't working for various reasons. They need the http:// in front of them AND the file must exist at the source (cannot be a 404 Not Found Error). Also, the extra semi colon in the comments you posted seems to indicate that it wouldn't even be compiling so you might be running a prior version. I created a new project and as an example ran this code which worked to download the google logo from the main page.
My.Computer.Network.DownloadFile("https://www.google.com/images/srpr/logo11w.png", Path.Combine(My.Computer.FileSystem.CurrentDirectory, "logo.png"))

How do I pass Form1.TextBox1.Text to Form2.TextBox2.Text using Visual Basic (correctly)?

This is a continuation of a question asked two years ago in this thread:
VB6 equivalent of string.IsNullOrEmpty
(I think.) A programmer recommended I use the String.IsNullorEmpty method, which I used this thread for to convert to Visual Basic, but I still couldn't get it to work.
The specifics of my question are here, including all current code:
http://www.daniweb.com/software-development/visual-basic-4-5-6/threads/473930/passing-data-between-forms-in-vba
Here is the gist of it, copied directly from the second link:
So I'm trying to make a link between TextBox1.Text on Form1 to TextBox2.Text on Form 2. What I currently have is a line of code underneath my TextBox2_Change code reading:
TextBox2 = Form1.TextBox1.Text
This ALMOST does what I want it to do. The only problem is that it is requiring me to input any character in to the TextBox2 when Form2 pops up before it displays.
I'm trying to get that problem solved and then I'm eventually going to try to get it to chop off part of the file name until just the project file name displays.......but that's a whole different game I'll be playing. One step at a time.
Does anyone have any suggestions?
Explanation
You should write the code under the Form2_Load event.
If you write the code under Textbox2_TextChanged event, the code will be executed only when you type or delete something in Textbox2 (That is the same as Text being changed).
Code and Example
Private Sub Form2_Load () Handles Mybase.Load
Textbox2.Text = Form1.Textbox1.Text
End Sub
Hope it works perfectly!
The behaviour you observe is normal : your TextBox2 is only updated (with the value from TextBox1) ... when you update it manually (_Change).
Hey friend it very simple. you need not to add any kind of other functions just use dot(.) operator to access all components of Form1.
e.g.:
Form1.TextBox1.AppendText("hello")
or you can read value from Form2 and insert it into Form1.
e.g. :
Dim txt As String=TextBox1.Text
Form1.TextBox1.AppendText(txt)