I've got a Windows Phone 7 Silverlight app that I am trying to use Isolated Storage on the emulator, to no avail. I've got several files added to the project as Content, I've deployed the app to the emulator before launching the debugger, I use the back button to "close" the debugging session, and I do not close the emulator between runs. And every single time, I have empty isolated storage. What am I doing wrong? I use this, comes up blank every time.
private void CheckIsolatedStorageForContent()
{
using ( var isf = IsolatedStorageFile.GetUserStoreForApplication() )
{
foreach ( var fileName in isf.GetFileNames() )
{
listBox.Items.Add( fileName );
}
}
}
Just putting them in your XAP as content won't deploy them to Isolated Storage.
On the first execution of your app, you'll need to copy the files to the Isolated Storage. If they're static, there's really no need to do this, as you can just access them as content. If you need to update them, then you will need to get them to the IsoStore.
Related
I'm creating a MAUI app and I'm trying to connect it to Firestore.
I have set everything up in Firebase and I can successfully connect to the Firestore by setting the environment variable: "GOOGLE_APPLICATION_CREDENTIALS" on my windows machine locally.
However, I cannot make it work if I test out the app using my local android device since this device doesn't have the environment variable set up.
So my question is, how would I go about doing this, so I can connect to the Firestore on my android device?
Some thoughts:
I would imagine that I need to set the environment variable in the MAUI code and somehow read it, but wouldn't that expose the secret file since it would now exist inside the MAUI project?
It looks like some people are using the google-services.json file for this purpose (which is not strictly secret) so if I could use that instead, then I guess that would be the best, but again, how would I do that?
I have tried adding the files in the Asset files, .csproj file, setting environment variables in the MainProgram etc. but I just can't make it work and even worse, I'm unsure whether that approach would be insecure.
Any help would be greatly appreciated.
Possible duplicate: How do I connect my MAUI app to a Firestore database? (via service account json file)
I just still don't understand how they made it work.
This is how I successfully set it up in .NET Maui:
Copy the "Json" file from Google under the "Resource/Raw" folder
with a BuildAction "MauiAsset"
Below is my initialization code:
public async Task<FirestoreDb> initFirestore()
{
try
{
var stream = await FileSystem.OpenAppPackageFileAsync(Constants.FirestoreKeyFilename);
var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
FirestoreClientBuilder fbc = new FirestoreClientBuilder { JsonCredentials = contents };
return FirestoreDb.Create(Constants.FirestoreProjectID, fbc.Build());
}
catch (Exception)
{
throw;
}
}
Hope that helps.
I am creating an app that is tracking GPS data (latitude, longitude, altitude). So far I've managed to create a listbox that gets an extra line everytime another set of coordinates is made.
I tried writing it to file with this function.
private async Task WriteToFile()
{
string ResultString = string.Join("\n", locationData.ToArray());
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(ResultString);
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var dataFolder = await local.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);
var file = await dataFolder.CreateFileAsync("DataFile.txt", CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenStreamForWriteAsync())
{
s.Write(fileBytes, 0, fileBytes.Length);
}
}
I can read this file, but I can't view this "DataFile.txt" anywhere in Files app.
I tried using WP Power Tools, but it doesn't work with 8.1, I am unable to update Visual Studio 2013 in order to get ISExplorer.exe working and
IsoStoreSpy keeps crashing everytime I try to connect my Lumia 620.
But all of this looks too complitated to me. Is there any other way of getting this .txt file without messing with IsolatedStorage? I feel like I'm missing out on something so simple here, I just can't believe that such basic thing as writing output to .txt, that can be later used by PC, couldn't be available.
You're storing the file in your app's local storage (Windows.Storage.ApplicationData.Current.LocalFolder), which is the same as Isolated Storage.
The Files app can see only public locations not app-specific locations.
There are several ways your app can share this file more globally:
Use the share contract to let the user share the file to wherever they'd like (OneNote, Email, etc.). See Sharing and exchanging data
Let the user choose where to save the file with a FileSavePicker. See How to save files through file pickers
Save the file on the SD card. See Access the SD card in Windows Phone apps.
Save the file to the user's OneDrive. See Guidelines for accessing OneDrive from an app
Save to a RoamingFolder so the file can be read by the same app on a Windows PC, which can then export using similar methods (especially a file picker) but on the desktop device. See Quickstart: Roaming app data
My application have a small piece of code which read/write a file in C++/CX (Windows Phone 8 Interop DirectX) like this:
bool WriteState(char *fileName) {
FILE *fp = fopen(fileName);
if (fp) {
// Do save
...
fclose(fp);
return true;
}
return false;
}
and ReadState has the same code. and invoking (call from C#):
ObjectClass game = new ObjectClass();
game.WriteState("game1.state");
It's no problem when I run on my device, debug with no error. But after I published to Windows Phone Store and install to my device, the Read/Write code doesn't work. Why this happen?
Make sure you aren't saving the file in its install location. Your app won't have access to its install location in production. You'll need to save in isolated storage.
http://msdn.microsoft.com/en-us/library/windows/apps/jj681698(v=vs.105).aspx
There is this way, that should work for you..
I have this solution that works perfectly on my development workstation using Visual Studio 2012 .Net 4.5. No IIS changes. It's basically C# that grabs a webpage and turns it into bmp. I'm not sure if my problem is that I'm trying to write to filesystem.
This is what I get in Azure. And I'm reading it might be a limitation/restriction in Azure (Very disappointed if that is the case)
502 - Web server received an invalid response while acting as a gateway or proxy server.
I figure my problem with this code:
public Bitmap GenerateThumbnail()
{
Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ThumbnailImage;
}
private void GenerateThumbnailInteral()
{
WebBrowser webBrowser = new WebBrowser();
webBrowser.ScrollBarsEnabled = false;
if (this.Method == ThumbnailMethod.Url)
webBrowser.Navigate(this.Url);
else webBrowser.DocumentText = this.Html;
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (webBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser.Dispose();
}
or maybe with image.save (which might be a problem next).
image.Save(Server.MapPath("~") + "/output/"+filename);
We are low budget and trying to keeping our hosted site simple and inexpensive. Would rather not have to build a worker process or use storage features of Azure.
===
UPDATE:
I don't think my issue is the Save, but do think that will be an issue on Azure. Additionally, as a test, I created a handler ashx that returns a image stream to a html img tag. That too works great locally, but on Azure I just get red Xs. Not sure what's up with that.
You basically don't have permissions to do that in Azure to write to the local directory without assigning them.
But there are many solutions to that problem.
Setup your start up task to assign permissions to the directory.
Worth reading this.
WindowsAzure: Is it possible to set directory permissions within the web.config?
If you need a temp local drive then this is preferred method.
http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx
If you need a persistent storage then Blob storage would be the best bet.
http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/
You can I believe also attach a drive to the compute like a VHD but I am not sure if two computes can both write to that drive at the same time so you would be maybe best to use blob storage if you need to keep the data.
Blob storage is super cheap.
hths, James
I have been using ApplicationDeployment.CurrentDeployment.DataDirectory to store content downloaded by the client at runtime which is expected to be there every time the app launches, however now I've found this changes seemingly randomly if the application is updated.
What is the best reliable method for storing user data for the application in click-once deployments?
Currently I've been using the following method
private const string LocalPath = "data";
public string GetStoragePath() {
string dir;
if (ApplicationDeployment.IsNetworkDeployed) {
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
dir = Path.Combine(ad.DataDirectory, LocalPath);
} else {
dir = LocalPath;
}
return CreateDirectory(dir);
}
I originally followed the article Accessing Local and Remote Data in ClickOnce Applications under the heading ClickOnce Data Directory which states this is recommended path.
NOTE: CreateDirectory(string) simply creates a directory if it doesn't already exist.
I have found the root cause of my problem is I'm creating many files and an index file, this index file contains absolute paths, click-once moves the content (or copies) on an upgrade, so the absolute paths no longer exist. I will investigate isolated storage as Damokles suggests to see if this has the same side affect for click-once deployments.
Another option is to make a directory for your application in the user's AppData folder and store it there. You can get a path to that with this:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
You'll find a lot of applications use that (and it's local equivalent). It also doesn't move around between ClickOnce versions.
Check out IsolatedStorage this should help.
It even works in partial trust environments.
To keep you data you need to use the application scoped IsolatedStorage
using System.IO;
using System.IO.IsolatedStorage;
...
IsolatedStorageFile appScope = IsolatedStorageFile.GetUserStoreForApplication();
using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream("data.dat", FileMode.OpenOrCreate, appScope))
{
...
code taken from this post
It depends on the data you are saving.
You are currently saving to the Data Directory which is fine. What you need to be aware of is that each version of the application has its own Data Directory. When you update ClickOnce copies all the data from the previous version to the new version when the application is started up. This gives you a hook to migrate any of the data from one version to the next. This is good for in memory databases like Sql Lite or SQL CE.
One thing that I cam across is that when you have a large amount of data (4 gig) if you store it in the Data Directory this data will be copied from the old version to the new version. This will slow down the start up time after an upgrade. If you have a large amount of data or you don't want to worry about migrating data you can either store the data in the users local folder providing you have full trust or you can use isolated storage if you have a partial trust.
Isolated Storage
Local User Application Data