I created a .txt file with data to load into a LongListSelector, but I cannot figure out how can I read the data from it. Using StreamReader with exact file path, the app crashes and with IsolatedStorage, it doesn't seen the .txt file.
Is there a way to accomplish this?
Where is your .txt file located, is it in the Isolated Storage?
If it's not, your Silverlight application will need to be "Trusted" (more on trusted applications).
If it is in the Isolated Storage, then you should not have any issue reading it using something like this.
var store = IsolatedStorageFile.GetUserStoreForApplication();
try
{
if (store.FileExists(path))
{
using (var stream = store.OpenFile("fileName", FileMode.Open))
{
//Read your file
}
}
}
catch (Exception e)
{
//Handle Exception
}
Keep in mind that Isolated Storages are separate for each application. If your file has been created by another application in its Isolated Storage, you won't be able to access it.
Related
I have a sqlite database file "test.db".
I want to zip this file through C# code.
But when I am trying to do this, I am getting "Access is denied" exception.
Here is the code that I am using :
byte[] buffer = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(fileToCompress));
ZipArchiveEntry entry = zipArchive.CreateEntry(fileToCompress.Name);
using (Stream entryStream = entry.Open())
{
await entryStream.WriteAsync(buffer, 0, buffer.Length);
}
Can any one tell how I can zip a database file in winrt app through C# code ?
I'm away from my pc here so I cannot try this but:
Windows RT apps cannot access files in the system outside the windows-user folders (Videos, Music, Documents, Downloads, Pictures and USB units) from code. This is only possible by using a file picker (this way, the user is responsible for the file selection).
If you try to do so from code, you get an exception.
Have you tried that piece of code having the "test.db" file inside the current user "Documents" folder?
The other idea that comes to mind, is to check that the Database is not opened and has been detached from your DBA.
I currently have the code below in my WPF application which does exactly what I want it to do, however, upon publishing this it won't necessarily be able to access these folder locations as they won't be pointing to the correct directory nor will the folders exist.
I was hoping somebody might be able to tell me what is the best way to save something into a local folder?
Whether it's inside the application folder itself or not is of no issue either.
The code I'm currently using for the writing of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, templateList);
}
The code I'm currently using for the loading of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
templateList = (List<Template>)bin.Deserialize(stream);
}
You could use System.Environment.SpecialFolder.LocalApplicationData to store application specific data:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
}
}
Ref: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
You can use Environment.SpecialFolder to find an appropriate place to put files (for example, ApplicationData would be a good place to start). If you only need a temp file, you can use Path.GetTempFileName to create one.
Edit: One last note. Storing stuff in the application folder itself can be a giant pain. Usually the application folder is created with the admin account during the installation so your app won't be able to write to it while running on a user account.
I am new to Silverlight and I am trying to do a directory listing of the contents of a directory. However when the first list of this code runs, it throws an exception:
The application itself runs inside of a Browser.
File operation not permitted. Access
to path 'C:\Program Files\AppName' is
denied.
I checked permissions and they are readable so I'm not sure why it's not working.
DirectoryInfo di = new DirectoryInfo(#"C:\Program Files\AppName");
try
{
if (di.Exists)
{
Console.WriteLine("That path exists already.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally { }
Silverlight doesn't let you just access any old directory you want. Silverlight 4 added the ability to access certain well-known paths under the user profile but only in out-of-browser elevated trust applications.
Silverlight is probably not the technology you want to use for this purpose. Look into WPF instead.
If you want to save/load files in silverlight and your app owns the files, you can use isolated storage.
silverlight isolated storage example
Generally, this is useful to store cross-session data locally, or store user settings. Like Josh's answer says, there is no solution to your problem if you need to access other app's files.
I have a doubt from a silverlight application we can access MyDocuments. I am creating an Application which will download a set of files from a remote server . Is it possible to save these file in MyDocuments instead of Isolated Storage. I am using Silverlight 4.0 . Can any one give me Sample codes for it.
In order to acheive that you need to use Silverlight 4 and specify that is should get elevated privileges when install as an Out-of-browser application. When running as an OOB the app will have access to the users Documents folder.
In all other cases you will need to use the SaveFileDialog where the user can explictly specify where to save the file.
Edit code example:-
if (Application.Current.HasElevatedPermissions)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Combine.Path(path, "MySaveFile.dat");
using (var filestream = File.OpenWrite(path))
{
// pump your input stream in to the filestream using standard Stream methods
}
}
No Isolated storage is currently the only option.
Im trying to clone facebook image uploader which is built in java. But I would like to use silverlight so Im wondering if I can somehow read local directory.
If I have this running an some remote server I can easily read the content of that server as I have C# as backend. But Im not sure how could I read certain directory of the user which is using silverlight application.
Any ideas if this is possible or not?
It's possible to read file "blindly" using OpenFileDialog. Blindly means you can let the user point the dialog to the file so Silverlight can read its content but it can't tell where the file is located.
Example:
var fileDialog = new OpenFileDialog();
var dialog = fileDialog.ShowDialog();
if (dialog.HasValue && dialog.Value)
{
byte[] bytes;
using (var fileReader = fileDialog.File.OpenRead())
{
bytes = new byte[fileReader.Length];
fileReader.Read(bytes, 0, (int) fileReader.Length);
}
}
The access to the file system is limited for security. Some access (blind as well) can be done using Isolated Storage where you can store data and access later.