How to access a file within program file without the full path - c++-winrt

I am using c++/winrt within a WinUI3 project and I am trying to have the application pull up a text file where users can select an option from it. The file I am trying to reach is in the project folder and included in the project. Currently, I have the file path set to my device so it pulls the text file from my directory. I want the application to be able to read from the program files instead of the long file path that is currently coded. I tried to change the relative path of the file and used the fstream function to read the file. I also tried just using "Aircrafts/Aircrafts.txt" but that did not work either.
Here is a snippet of the code.
fstream aircraftFile;
string info;
void MainWindow::loadPlatformData()
ifstream aircraftFile; //File Object for list
aircraftFile.open("C:\\Users\\TimmyK\\Documents\\GitHub\\sentinel3\\Sentinel3\\Aircrafts\\Aircrafts.txt", ios::in);

My guess is that you are writing a UWP project which is why the full-path access to your file fails. UWP's have restricted access to the file system per Microsoft Docs.
For a UWP project, you mark the file as Content Yes in the file properties so it's placed into your application's layout package. Normally the 'current working directory' is pointing to your packaged program's installed location. You can get a full path to this directory using:
auto installdir = Windows.ApplicationModel::Current().InstalledLocation();
std::wstring str = installdir.Path().c_str();
For Win32 "classic" desktop applications, there's no specific packaging or installation solution so there's lots of different ways to do it. In Visual Studio when you start the debugger or run a program, the "current working directory" is going to be the project directory but if you run the EXE from the command-line, it will be in a different directory.
To find the running EXE's directory per IInspectable, use GetModuleFileName:
wchar_t exePath[MAX_PATH] = {};
DWORD nc = GetModuleFileNameW(nullptr, exePath, MAX_PATH);
if (nc > MAX_PATH || nc == 0)
{
// Error condition
}
A typical pattern is to look in the EXE folder, then 'walk up' the directory to find asset files. This is what we do a lot in DirectX samples for Win32. See FindMedia.

Related

What's the best way to place a local SQLite database file in a WinForms application to publish the project on GitHub and distribute the executable?

I'm trying to push to GitHub a Windows Forms VB.Net application which does CRUD operations on a local SQLite database file, but I realized the db3 file and System.Data.SQLite.dll are excluded from the push because the contents of bin/Debug folder where they're located are a part of the .gitignore policy template on Visual Studio, which absolutely makes sense.
I have tested the .exe file out of this Debug development folder and couldn't run the application either, throwing the exception: "Could not load file or assembly 'System.Data.SQLite, Version=1.0.116.0, Culture=neutral, PublicKeyToken=...' or one of its dependencies"
My Problem is all the guides I've checked to use a SQLite in a Windows Forms project place the database file in bin/Debug, I guess for the simplicity of the tutorial. So this way of referencing the SQLite db3 in bin/Debug works fine in my application:
Public dbFullPath As String = Application.StartupPath & "\\myDatabase.db3"
Public conStr As String = String.Format("Data Source = {0}; version = 3;", dbFullPath)
However, I don't know how to place the database at the same level of other project files like .vb Forms and the Resources folder and reference it so the app works and all the necessary files are pushed to my public repository.
I created an App_Data folder at project level and placed the database file inside and then tried to replace the path String from:
Public dbFullPath As String = Application.StartupPath & "\\myDatabase.db3"
to this:
Public dbFullPath As String = "App_Data\\myDatabase.db3"
But I'm unable to open the database in a different place other than bin/Debug.
Thank you very much for your help.
In order to run the application outside of the visual studio environment you will need to copy both the x64 and x86 folders which are generated at build time
In Chem4Word we use the NuGet package SQLite 1.0.117 to read and write to a SQLite database which is deployed to C:\ProgramData\Chem4Word, but it could be anywhere in the file system to suit your purpose.
Our installer also deploys the contents of the above folders (x64 and x86) beneath the application folder in C:\Program Files (x86)\Chem4Word as shown below
The "seed" database is stored as an embedded resource, hence gets checked in to the repository.

Where does intellij idea save the local storage and preferences of libgdx (pc)

I started working with files, a simple operation of writing and reading files.
But i had an error when writing a file and now i have to fix it by hand.
Thats the problem, i don't know where is my file.
Also i would like to see the file i'm writing.
I am working with intellij idea 2016 1.4, maybe the file is complied in a jar?
Yes, i know that clearing cache its an option.
nothing here: https://github.com/libgdx/libgdx/wiki/File-handling
on the wiki link only talk about where you can find the ablolute path file but thats not my case. I get the file this way:
this.resolver = Gdx.files.local(path + "item"+ String.valueOf(weaponNumber) + ".txt");
String description = this.resolver.readString();
So.. where is the file? thanks
In the desktop version it saves the file in the assets folder inside your android module.
FileHandle file = Gdx.files.local("myfile.txt");
file.writeString("Test libGDX", false);
System.out.println(Gdx.files.getLocalStoragePath());
Output: D:\Dropbox\Projetos\Outros\gdxTest\android\assets\
The project folder in my computer is gdxTest.
In your case you have the path var so probably will be a folder inside assets folder.
But when you pack the desktop game into a jar file, the file will be created in the same folder where your game jar file is located. Usually yourProject\desktop\build\libs.
The difference is because when we configure the desktop project we set the Working Directory in the yourProject\android\assets\ folder. So to Android Studio it is the local folder of your project.

How can we access the files in the Data Folder when we publish the Vb.net application

I have added some files that I need to be downloaded to the Application start up path. So I set Build Action as content now the files have been copied some where
C:\Documents and Settings\TestUser.ANNAM\Local Settings\Apps\2.0\Data\HVDRBMY5.8AA\858AT9VM.TNP\test..tion_2d7cfc137d9c2c74_0001.0013_432bd4561850d290\Data
How can access file from the application. My problem since it is a dynamic path will it be same folder count so that we can use like ....\Data\ Some think like this
You can use My.Application.Info.DirectoryPath this will return the directory where the application is stored.

How can we access the files in the Data Folder when we publish the Vb.net application

I have added some files that I need to be downloaded to the Application start up path. So I set Build Action as content now the files have been copied some where
C:\Documents and Settings\TestUser.ANNAM\Local Settings\Apps\2.0\Data\HVDRBMY5.8AA\858AT9VM.TNP\test..tion_2d7cfc137d9c2c74_0001.0013_432bd4561850d290\Data
How can access file from the application. My problem since it is a dynamic path will it be same folder count so that we can use like ..\..\Data\ Some think like this
Application.UserAppDataPath gets the path for the application data of a user.
Application.StartupPath gives you the path for the executable file that started the application, not including the executable name.
Starting with one of these, you should be able to use System.IO to manipulate the paths until you get the folder where your data files are.

adding jpegs to vb.net application

i am using itextsharp and creating a PDF with images.
currently the images i am using in the application are on my desktop, but i will need to make an installation file that will put the images in a specified directory on the users computer and be able to call them from the specific directory.
how do i include pictures with my build?
how do i reference the pictures? currently i am using:
Dim jpeg3 As Image = Image.GetInstance(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\2.jpg")
How and where do you add the pictures?
If you simply added them to the project as if they were code files (using "add existing item"), then check the Properties for the file in the solution explorer (Build Action, and Copy To Output Directory are useful), and also the Application Files button/window (under the project settings -> Publish). This is useful for distributing the files along with the application (for both debug and release), deployed in a specific sub-directory.
If you added the files to the project's resource file, you can use them using the My.Resources namespace.