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.
Related
I am in process of building an app which writes data continuously to a file. When I start the program the file is created and starts being written to.
However I noticed that sometimes if I have Windows Explorer open, access to the file is denied to my app, and an error is thrown.
fs = new System.IO.FileStream(location,
System.IO.FileMode.Open,
System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
So how do I restrict access to this file so only my app can access it, not any other programs?
You can change the last parameter from System.IO.FileShare.ReadWrite to System.IO.FileShare.None.
That locks the file in location exclusively as long as this stream is open and no other app can read, modify or delete that file except your own FileStream.
In fact this isn't only true for other apps - even your own app can't open another FileStream of that file. So keep it open as long as you need but don't forget to correctly dispose the FileStream after use.
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.
I am working with Store apps using C#.
I am using StorageApplicationPermissions.MostRecentlyUsedList to load Local Epub files.
After getting those file as a Storage file.
string EpubPathToken= Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file);
StorageFile file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(EpubPathToken);
Extraction(file);
While Extracting the Epub file as Zip to Package folder using ApplicationData I am getting an Exeption of Unauthorized.
Here's screen shot of error
Why because If I use the same code using from KnownFolders like Pictures Library the Extraction is working Fine.
Can any one give me any idea about this ?
Finally I solved the issue of getting exception of Unauthorized With the use of Blog
I changed the code snippet according to my requirement.
The source of code describes writes bytes of file in ZipArchiveEntry,
hence I used a helper method GetByteFromFile(), which takes StorageFile object and returns me byte[] array.
Finally thanks to Xyroid
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.