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.
Related
I want to create an application where the user after authentication can upload a file(txt/xml etc.) using the Azure Mobile Services and after that he can download only those files which were uploaded by himself.
I've watched a lot of tutorials (including this one: link ) but in this case they simply inserts a row to a database table. I want basically the same thing, just with files. How can I do that?
I'm really new to this, so I'm just guessing, but should I upload the files to Blob Storage, and store a link in the database pointing to that file? I'm searching for the best practice.
Yes, you are correct!
You would be limited in size if you tried to store the text file as a field in the database.
http://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-store-dotnet-upload-data-blob-storage/
Shows how to do what you want to do but with images.
You would want to change the image stream to a text stream here:
// Get the new image as a stream.
using (var fileStream = await media.OpenStreamForReadAsync())
{
...
}
And use the Stream classes instead to open the file stream:
http://msdn.microsoft.com/en-us/library/windows/apps/system.io.stream(v=vs.105).aspx
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 am developing an application which keeps document repository and provide searching from it. I want to prevent users to view/add/modify/delete documents outside application. Currently, i am storing documents in a normal folder structure in windows, which is easily accessible to any authorized windows user. I am seeking for some methodology using which, I can ensure that, none other than my application can access those documents.
Is there any library or technique available, using which I can hide those files from windows user, but my application can use it in normal way?
If you have to save documents in the windows and want to restrict users then try saving them in encrypted form with possibly a password. So that even if someone has access to it, it becomes useless for him. Something like this should work for encryption
using (var fileStream = File.OpenWrite(theFileName))
using (var memoryStream = new MemoryStream())
{
// Serialize to memory instead of to file
var formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, customer);
// This resets the memory stream position for the following read operation
memoryStream.Seek(0, SeekOrigin.Begin);
// Get the bytes
var bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
var encryptedBytes = yourCrypto.Encrypt(bytes);
fileStream.Write(encryptedBytes, 0, encryptedBytes.Length);
}
And you can use this library for password protection and zipping
If you are on Windows Server 2012, you can use Windows Server 2012 Dynamic Access Control which brings 2 features called SDDL (Security Descriptor Definition Language) and DACL (Dynamic Access Control).
Here are a couple articles worth reading:
http://blogs.technet.com/b/wincat/archive/2012/07/20/diving-deeper-into-windows-server-2012-dynamic-access-control.aspx
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/SIA341
With dynamic access control, you can define which users view / edit which files and folders based on classification and user claims.
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 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.