How to use String name in StringBuilder.AppendLine()? - vb.net

I'm trying to use string name in appendline() function. The string named "programFiles" gets the program files location correctly, but I need to include it in append line (to write the path to the file) Tried this,
Dim programFiles As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Dim sb As New StringBuilder()
sb.AppendLine("Your " & programFiles & "is working properly.")
File.WriteAllText(Environment.GetEnvironmentVariable("appdata") & "\mytext.txt", sb.ToString())
But it doesn't write to a file. What is the correct way to do that ?

When I tried your code on my machine, it worked as I would have expected, the file c:\users\[myusername]\Appdata\Roaming\mytext.txt was created and contains one line
"Your C:\Program Files (x86)is working properly."
so obviously your VB is correct. Maybe you're looking in the wrong subdirectory?

Related

How to declare a path without the drive letter?

Example i have picture folder located at
Dim path As String = "D:\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Something like that. now i want the the picture folder to just paste it in debug folder can i call it without the drive letter something like this ??
Dim path As String = "\bin\debug\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Its not working. The reason why i want to achieve this is example i use my program in another computer but the other computer don't have drive D: then my program will not work because in my code all of the student picture declared in drive D: thank you so much.
You can get the application startup path and create the folder in either the Debug or Release locations, depending which you're running. The folder would be created within the directory in which the end-user runs the compiled application.
Dim PicPath As String = Application.StartupPath & "\Student Picture
If Not My.Computer.FileSystem.DirectoryExists(PicPath) Then
My.Computer.FileSystem.CreateDirectory(PicPath)
End If
PicPath &= "\Student-" & textbox_Search.Text & ".jpg""
Small edit- I realised you wanted to include file name as well. Please do not use "+" when concatenating strings, the proper operator is "&" use the "+" operator only when youre doing math and some other unique cases, such as datatable expression column concatenation.

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

How to open a file from folder where EXE was opened. VB

Part of a program I am making I need to open a file (for example a txt file) from the folder where the program was opened.
The idea is that it can be zipped up and put anywhere without having to place the file in a certain location.
It's got to be Visual Basic and I will really appreciate some help.
I have googled this but found nothing for VB. I'm relatively new to the language.
Thanks, Jack
To open the file do this:
Dim fileName as String = "yourfile.txt"
Dim appDir as String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Process.Start(appDir & "\" & fileName)
You can use this to get the path to the folder where the currently executing assembly (i.e. the EXE) is located:
System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, assembly.Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar))

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.

Renaming a file in %appdata% in VB.net

So I need somone to tell me how to fix this code. I'm trying to rename a file which is in C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar to minecraft.jar.
The code I am using is:
My.Computer.FileSystem.RenameFile("C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar", "minecraft.jar")
Can someone fix this?
%appdata% not not a valid path, rather it denotes a special folder that you can get by using Environment.GetFolderPath, once a get the %appdata% path, you can easily rename file.
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim file_to_rename = Path.Combine(folder, ".minecraft\bin\XenonUpdate.jar")
My.Computer.FileSystem.RenameFile(file_to_rename, "minecraft.jar")
File handling functions do not deal with environment variable expansion, %appdata%. You need to do this yourself.
My VB.Net is non-existent, but I think it would look like
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim from = path + "\.minecraft..."
Dim to = path + "\.minecraft..."
My.Computer.FileSystem.RenameFile(from, to)
Also, see C# getting the path of %AppData%