How to create a dynamic excel link in VB.NET? (not .dll) - vb.net

VB.NET level: Beginner
I made a .exe using VB.NET. In this program there are many links to excel files (There are specific folders for excel files).
My problem:
Consider an excel file named as abc.xlsx, which is on my home pc. Link to this file is as follows,
D:\work\data\abc.xlsx
now for obvious reasons, this link will not be valid when I run the .exe on my work pc.
(Later I want to run this .exe on multiple pc's)
How to solve this issue?
My thinking is to create a dynamic link which will update itself based on pc in use or to create a constant link which is independent of pc in use.
Help will be really appreciated.
Thanks in advance

so make the path relative to the .exe
C:\myapp\myapp.exe
c:\myapp\data\abc.xslx
...
So no matter where you app is, you can get to your data like this
Dim dataFolder As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location)
dataFolder = System.IO.Path.Combine(dataFolder,"data")
Dim theFileIwant as String = System.IO.Path.Combine(datafolder,"abc.xslx")

Try something like this:
Public Function GetDynamicFilename(p_filename As String) As String
Dim tempPath As String
Select Case My.Computer.Name.ToUpper
Case "COMPUTER1"
tempPath = "c:\work\data"
Case "COMPUTER2"
tempPath = "d:\work\files"
End Select
Return String.Format("{0}\{1}", tempPath, p_filename)
End Function

Related

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.

.NET find .xls or.xlsx file with GetFiles using wildcard? example: .xls*

I need to find an Excel file. However, the extension of the file I"m looking for could be .xls or .xlsx. I was considering using FileExists but I can't use a wildcard with that. Here's my attempt at using GetFiles, however, the .xls* part of my code does not work. I've never used GetFiles before, can anyone give me some guidance on what I'm doing wrong?
Dim InputFormPath As String = "W:\TOM\ERIC\NET Dev\"
Dim wbNameXLSInputForm As String = StatVar.xlApp.Sheets("New Calculator Input").Range("D15").Text & ".xls*"
Dim XLSInputForm As String = wbNameXLSInputForm
Dim dirs As String() = Directory.GetFiles(InputFormPath, wbNameXLSInputForm)
If dirs.Length <> 0 Then
'do something
End If
Take a look at this documentation. It says: The following list shows the behavior of different lengths for the searchPattern parameter: "*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.

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%

Creating a directory named with user's text in Visual Basic

I'm fairly new to programming in general. I'm working on a simple app that could combine a couple of functions to automate or simplify some of the things I do at work. One of the functions I'm trying to build is to be able to create a folder. Now I have found an article on that on Microsoft's msdn resource and it's child's play. But the instructions there only show how to create a folder with a predefined name in the code. What I'd rather want is to have a textbox where I input the folder's name and the directory is named with that input. The msdn code looks like this:
My.Computer.FileSystem.CreateDirectory _
("C:\vb\")
I understand I should now add:
Dim txt As String
txt = TextBox1.Text
But what next? How do I tell VB to use as directory name the input "txt"?
Try this:
Dim txt As String
txt = TextBox1.Text
My.Computer.FileSystem.CreateDirectory("C:\" & txt & "\")
Using & is simple and often fine for most purposes, but for your two (including what you've added as a comment) concatenation examples there are other methods:
For pathname manipulation look at System.IO.Path:
My.Computer.FileSystem.CreateDirectory(Path.Combine("C:\", txt))
For (complex) string "formatting", consider String.Format:
Dim menu As String = String.Format("Today's main dish is {0}.", TextBox2.Text)