VB.net Const - Project location path - vb.net

I am using a SDK with the SDK one of the parameters calls for a const which is a file path to a DLL. The example that I was given hard codes the file path but I need this to point to the project path so that when I deploy I can deploy it anywhere.
I have tried to use a couple of functions to get the file path of the project but because I need a const I get micorsoft error. "Constant expression is required. "
Public Const strLibrary As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "\E_BEPSET32.DLL")
Is it possible to achieve this or do I have to hard code the value?

Related

Always "requiring unknown module" when reading json file

bit of an RN newb here. I'm trying to read some json data files:
function loadCategories() {
const ids = ['tl1', 'tl2', 'tl3', 'tl4', 'tl5', 'tl6'];
ids.forEach(function(id) {
var contents = require('../Content/top-level/' + id + ".json.js");
...
});
}
But here I always get an error:
Unhandled JS Exception: Requiring unknown module "../Content/top-level/tl1.json.js".If you are sure the module is there, try restarting the packager or running "npm install".
The files exist and my relative path logic should be OK given the project structure:
ProjectDir
Components
ThisComponent.js
Content
top-level
tl1.json.js
tl2.json.js
...
i.e. the above code is running from ThisComponent.js and trying to access tl1.json.js, etc so I would think the relative path of ../Content/top-level/tl1.json.js would work.
I've tried:
Restarting the packager
Referencing ./Content/top-level/tl1.json.js instead
Referencing /Content/top-level/tl1.json.js instead
I'm on RN 0.36.0. Gotta be something obvious…right?
This isn't possible in React Native because of how the packager works. You have to require files with static string path. You can use a switch statement something like this -
switch (id) {
case 'tl1': return require('../Content/top-level/tl1.json');
case 'tl2': return require('../Content/top-level/tl2.json');
...
}
Also why does your json files have .js extension?

How to find the path to the executable of a program

In a VB.NET application I need to set a button to start a program - specifically, Steam. But I need to allow for the user having installed the program somewhere other than the default location "C:\Program Files (x86)\Steam". So all I know is the executable name: "steam.exe".
In searching for an answer it looks like I should be able to get the path using Microsoft.Win32.Registry somehow, but the examples I'm seeing aren't helping me (I guess I'm just not getting how to apply those examples to my situation).
This tutorial gives the example of finding the path to the excel executable:
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MICROSOFT\Windows\CurrentVersion\AppPaths\excel.exe")
Dim path As String = regKey.GetValue("Path").ToString
But I'm getting a value of "Nothing" for regKey. Not only that, even if it did get a value for regKey, how would I know the registry path for another program (Steam, in my case)?
And how much will the version of Windows change the possible registry location of a program - if at all?

I can't get netbeans to find a txt file I have in the same directory... java.io.FileNotFoundException

I can't make it path specific because once I get this program to work (this is the last thing I have to do) I'm uploading to my university's ilearn website and it has to run on my professors computer with no modifications. I've tried a few different amalgamations of code similar to the following...
File file = new File("DataFile.txt");
Scanner document = new Scanner(new File("DataFile.txt"));
Or...
java.io.File file = new java.io.File("DataFile.txt");
Scanner document = new Scanner(file);
But nothing seems to work. I've got the necessary stuff imported. I've tried moving DataFile around in a few different folders (the src folder, and other random folders in the project's NetBeansProjects folder) I tried creating a folder in the project and putting the file in that folder and trying to use some kind of
documents/DataFile.txt
bit I found online (I named the folder documents).
I've tried renaming the file, saving it in different ways. I'm all out of ideas.
The file is just a list of numbers that are used in generating random data for this program we got assigned for building a gas station simulator. The program runs great when I just use user input from the console. But I can not get netbeans to find that file for the life of me! Help!?!?!?
Try adding the file to build path ..
public void readTextFile (){
try{
Scanner scFile =new Scanner(new File("filename.txt");
while(scFile.hasNext()){
String line =scFile.nextLine();
Scanner details=new Scanner(line).useDelimiter("symbol");
than you can work from there to store integer values use e.g in an array
litterArr(size)=details.nextInt();
Note: size is a variable counting the size/number of info the array has.
}
scFile.close();
{
catch
(FILENOTFOUNDEXCEPION e){
..... *code*
}
Keep file in the same folder as the program,but if it is saved in another folder you need to supply the path indicating the location of the file as part of the file name e.g memAthletics.Lines.LoadFromFile('C:\MyFiles\Athletics.txt');
hope this helps clear the problem up :)

Resolving relative paths outside the standard directories (applicationDirectory, desktopDirectory, etc)

I need to navigate to a file relative to my applicationDirectory, but as it says in the documentation:
No ".." reference that reaches the file system root or the application-persistent storage root passes that node; it is ignored.
But the crazy thing is that if I do something like
File.applicationDirectory.resolvePath("/home/myHome/");
I can get anywhere in the filesystem.
My question is:
is there a workaround to navigate from my applicationDirectory to a relative path like "../../my.cfg" ?? (I need to read a config file generated by a different application)
if you are trying to access root privileged folders - than you can not.
in other cases try do next "home/blah/blah/blah/../../my.cfg" and research once again http://help.adobe.com/en_US/AIR/1.5/jslr/flash/filesystem/File.html to save your time about navigation.
also you have another few ways: create a link to your file or run external bash/bat script.
I was previously using the little hack mentioned in Eugene's answer to copy from an absolute path:
var file = 'C:\Users\User1\Pictures\pic.png';
var newPath = air.File.applicationStorageDirectory.resolvePath('images/pic.png');
air.File.applicationDirectory.resolvePath('/../../../../../../../../../../' +
file).copyTo(newPath, true);
However, this is a much better way of doing it:
var file = 'C:\Users\User1\Pictures\pic.png';
var newPath = air.File.applicationStorageDirectory.resolvePath('images/pic.png');
new air.File(file).copyTo(newPath, true);

Executable directory where application is running from?

I need to get the path (not the executable) where my application is running from:
System.AppDomain.CurrentDomain.BaseDirectory()
When I run the above statement with & "/images/image.jpg" on my local machine it works fine but when I install the application on another machine it says it cannot find the file and there is a lot of extra path information some.
I just need the directory of where the app is running. I am coding in VB.NET with Visual Studio 2008.
Thanks!
This is the first post on google so I thought I'd post different ways that are available and how they compare. Unfortunately I can't figure out how to create a table here, so it's an image. The code for each is below the image using fully qualified names.
My.Application.Info.DirectoryPath
Environment.CurrentDirectory
System.Windows.Forms.Application.StartupPath
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly.Location
System.Reflection.Assembly.GetExecutingAssembly.CodeBase
New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)
Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))
Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))
---
Edit October 18, 2021:
Sigh... None of the above work if using net5.0 or net6.0 and publishing app as single-file bundle. Best I got now is:
// This will give you the directory but not the assembly
string basedir = AppContext.BaseDirectory;
// Before you package the app as a single file bundle, you will get the dll.
// But after you publish it, you'll get the exe.
string pathToExecutable = Environment.GetCommandLineArgs()[0].Replace(".dll", ".exe");
Dim strPath As String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Taken from HOW TO: Determine the Executing Application's Path (MSDN)
I needed to know this and came here, before I remembered the Environment class.
In case anyone else had this issue, just use this: Environment.CurrentDirectory.
Example:
Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)
When run from Visual Studio in debug mode yeilds:
C:\Development\solution folder\application folder\bin\debug
This is the exact behaviour I needed, and its simple and straightforward enough.
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
You could use the static StartupPath property of the Application class.
You can write the following:
Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")