I have a CRUD application using a local database (SQLite). My problem regarding saving this database is as follows:
When I use the following code:
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "Student.db3");
_dbConnection = new SQLiteAsyncConnection(dbPath);
works fine when testing the app on a windows machine and I have found the file here:
C:\Users\{myUsername}\AppData\Local\Packages\7BD31CA9-CFFD-4A21-9B24-A87481C6221D_9zz4h110yvjzm\LocalState
But when running this app on the android emulator, I have no idea where it is saved... I have searched the AppData directory back and forth and used the searching functionality of the directory, but nothing.
From anyone's experience, how can I save this database file in the current directory of the project itself to ease things up?
Notes:
I have tried using this path, to save the file in the current directory, but nothing when testing on the Android emulator.
AppDomain.CurrentDomain.BaseDirectory
It just seems to me that, the file is saved to a completely different when tested on the Android emulator but don't know where. Thoughts?
Is there any way to have the local database on the desktop machine, in the project's directory? Knowing that the database is in the android file system.
According to the documentation:
Returns the FilesDir of the current context, which are backed up using Auto Backup starting on API 23 and above.
Depending on how you deploy the app to the emulator, it should be in the same directory as binaries for the app.
See File system helpers - Platform differences
Related
I have an application that is being installed from MSIX installer.
It is installing successfully, but when execute it after installation i get an error that there is no such a file in C:/UserName/AppData/Roaming/MyApplication.
What i have tried:
Creating the folder (from C# code) on the very beggining of the application.
Result: It was creating it somewhere, but i couldnt see it on a disk apparently. Even when i tried creating files in that directory, i could do it without exception. So im sure that this folder existed. Somewhere. When i ran application from MSIX it gave me previously mentioned exception.
I created folder from the OS manually to see if the error will persist.
Result: I ran application from MSIX and everything worked fine.
At the last step i tried to access priviliges to the C:/WindowsApp folder (thats where MSIX are installing things), and i tried to run application from there manually by executing .exe (not by MSIX like previous times), to see if the error will last.
Result: It created those folders without problems, and application worked fine.
Any ideas what im doing wrong, or what i could do avoid this error ?
PS. I cannot change location of path C:/UserName/AppData/Roaming/MyApplication, because it is being created by 3rd parties dll's (Devexpress).
Try searching for the files under this path:
C:\Users\User\AppData\Local\Packages(hash)....
That is where all MSIX packaged apps redirect their AppData resources.
However, if the files are found to be existing in the "real" AppData folder, the app will work with that copy, not the one from the virtualized location (this is international behavior from Microsoft, to smoothen the transition of apps from classic installers to MSIX).
That is why the application worked after you created the folder manually.
Also, note that the files from the virtualized location are accessible only to your application and can't be accessed by others from the machine.
I suspect that when run the EXE directly from the install folder (not from the start menu entry point) the OS does not launch app in the container of the MSIX package, so none of the rules of running as a packaged-app apply. Therefore you can ignore this scenario.
Go back to #1 and try searching for the files under the path I mentioned. How are you (Devexpress) accessing the AppData path? I assume they are using something in the lines of (this should be ok):
Environment.GetFolderPath(Environment.SpecialFolder.)
I am writing a program in Visual C# 2015. I have created a local database from within the IDE and everything seems to work fine after building and running locally. However, after publishing the application and installing on another machine I get an argument exception error at attachdbfilename. I have included the database as a content file. The ClickOnce does copy the database to a folder under APPS in my Appdata folder. I have installed SQL Local DB edition on the target machine. The database name is ConnectionsDatabase.mdf.
These are definitions in my Settings.Designer.cs file.
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionSet" +
"tings.mdf;Integrated Security=True; User Instance=True")]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionsDa" +
"tabase.mdf;Integrated Security=True; User Instance=True")]
Not sure if they should both be there for just 1 database.
This is what I use for connection string within the appropriate classes.
using
(SqlConnection con = new SqlConnection(Store_Switcher.Properties.Settings.Default.ConnectionsDatabaseConnectionString))
I am willing to share any and all project files that are needed. Keep in mind this is a small application that will be installed on several different machines. Please help!! This is driving me crazy!
AttachDbFilename=|DataDirectory|\ConnectionSet" + "tings.mdf
should be
AttachDbFilename=|DataDirectory|\ConnectionData" + "base.mdf
in your designer file. or vice versa. you have a disconnect in file names.
EDIT:
You mean "invalid value for key"? The problem sounds like whatever path you are using for the database on the target system doesn't match your development machine.
You even say "The ClickOnce does copy the database to a folder under APPS in my Appdata folder. " So maybe on your dev box you have c:\myapp\App_Data\ConnectionData.mdf and on target it is c:\myapp\App_data\APPS\ConnectionData.mdf after it gets deployed.This won't work. Get rid of the APPS, or change your code so the deployment folder is the same as on your dev box. This is a path issue.
The documentation doesn't explain the behavior when passing in a path such as "myFile_temp.jpg" but I would assume that it would save the the application directory because this is a relative path, relative to the application we are currently running.
I think that the problem can be solved by prepending the current application directory to my temp file name using
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Sure there are lots of ways to do it, but this should work.
My issue is I'd like to know why this is happening rather than just throw a patch on it and ship it back out to the users.
Code is WPF, C# project compiled with .NET 4.0 and Visual Studio 2010 and runs on a lot of different machines. Mostly 32-bit XP,while the dev machine is a 64-bit Windows 7.
Can any one explain this behavior and why it's occuring?
Edit
The files will on occasion be saved to the directory the user selected files from to manipulate. They resize them, the program keeps track of the size percent for each of the file paths. When the user is finished they will click done and the program will go through each of the file paths, create a copy, resize the image and then save it with a _temp on the end.
Take note that it doesn't always do it and it when it does it doesn't do it for all the files they touched.
It works as s expected. You just didn't expect valid behavior. Lets assume that your app is placed in c:/superapps/myapp.exe. You opened command line and you're in C:\ which means that this your current working directory.
You can still run your app by ./superapps/myapp but your working directory is still C:\. And this will be working directory of your app in this case, not the directory you placed the binaries.
That is why it may not have permission or save data in some unexpected by you location. You should always think that your app could be run just like any other command like dir. It will be working in the place where user is currently standing (his current working dir) not in the place it's binaries are stored in
I am using C# with .net 3.5
I am saving my program data in a file under: C:\Program Data\MyProgramName\fileName.xml
After installing and running my application one time I uninstalled it (during uninstallation I'm removing all the files from "program data")
and then I reinstall the application, and ran it.
The strange thig is that my application started as if the files in program data existed - means, I had old data in my app even though the data file was deleted.
When running:
File.Exists("C:\Program Data\MyProgramName\fileName.xml")
I got "true" even though I knew for sure that the file does not exist.
The thing became stranger when I ran the application as admin and then the file didn't exist.
After a research, I found out that when running my application with no admin priviliges instead of getting: "C:\Program Data\MyProgramName\fileName.xml" I get "C:\Users\userName\AppData\Local\VirtualStore\ProgramData\MyProgramName\fileName.xml"
and indeed there was a file that existed from the previous installation (that I obviously didn't delete ,because I didn't know it existed).
So apparentlly there is some virtual path to the file under program data.
EDIT :
I found out that after deleting the old file in the virtual store, my application is suddenly able to find the correct file. (I didn't make any changes in the file under Program Data.
My question is:
why is it happen.
How can I prevent it from happening
Thanks in advance
Do you actually have to write to the per-system Program Data folder instead of the per-user Application Data folder(s)?
You might want to take a look at Environment.GetFolderPath and the following Environment.SpecialFolders:
Environment.SpecialFolder.ApplicationData - data folder for application data, synchronized onto domain controller if the user profile is roaming
Environment.SpecialFolder.LocalApplicationData - data folder for application data, local and not synchronized (useful for, for instance, caches)
EDIT:
Tested on Windows 7 x64, non-administrator user.
var appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var myFolder = Path.Combine(appData, "MyApp");
if(!Directory.Exists(myFolder)) Directory.CreateDirectory(myFolder);
File.WriteAllText(Path.Combine(myFolder, "Test.txt"), "Test.");
This does what is expected, ie. writes into C:\ProgramData\MyApp\Test.txt. As far as I can tell (Administrator mode Command Prompt), there's no UAC virtualization going on either.
Double edit:
I guess what's happened is that at some point an Administrator user has written the files into your ProgramData folder, and as such, UAC file system virtualization kicks in and redirects the non-administrator writes into the VirtualStore.
Does your uninstaller run as Administrator? If it does, you might have to check both the VirtualStore path for the user who initiates the uninstall, and the actual file system path for program data to remove. I'm not sure if there's an official way to do this, though...
I found the reason for the bug.
the application is trying to take ownership on the file and then the other file is created.
I removed that line and now everything works just fine.
I have a .NET CF project. In the project directory I put a simple xml file (users.xml) which has to be read by the device. When I debug the application on device emulator and try to load the file from code, the Exception is thrown (FileNotFoundException "Could not find file '\\users.xml'.").
Is there a mechanism to automatically deploy also configuration files to a device emulator?
You have you path set wrong in your code. After following the instruction of Shaihi or Sphynx to get Studio to deploy the file, the file is then in the folder with the application.
Based on the fact that you're getting an error that it cannot find "\users.xml" tells me that you're either telling it to specifically look in the root folder or you haven't specified a folder.
Windows CE requires that you provide a fully-qualified path, so your application should either use the full path it is deployed to (i.e. "\Program Files\MyApp\users.xml") or you need to construct the path:
Path.Combine(
Path.GetDirectoryName(
Assembly.GetExecutingAssembly().GetName().CodeBase
), "users.xml");
Here is how to do it in VS2005. It deploys the file along with the project.
This is for a native application - it should be similar for a managed one.
Content field specifies whether to deploy the file to the target. The file is deployed to the same directory as the application. I am not sure how VS2005 handles the deployment in case you updated the file, meaning you might need to manually delete it on the device.
VS2005 http://www.freeimagehosting.net/uploads/255e2922df.jpg
You can try adding the file to your application, setting "Build Action" to content, and enabling "Copy to Output Directory".
Also, I believe there's an option in emulator preferences which enables sharing files between emulator and the host OS.
Go to File->Configuration on the emulator and share a directory from PC. This directory becomes your storage card so if you put a file there like test.csv then you can read it from "Storage Card\test.csv" in the program. (I am using VS2008 and Windows Mobile 5.0 Pocket PC RC 2 emulator.