Creating/Writing/Zip/Unzip a file in UWP - c#

edit: About the path-problem. I will try to get there later. For now I just need help for zip a file. Could not find a way to do this yet.
Im currently going through a few basics and I don't know what I have to look for to get to where I want to be. So, what are my goals?:
I want to create a name.json file here C:\Users\Username\Desktop
Then I want to compress name.json to an zip file.
I also created another file Testfile.zip on my Desktop. I want to unzip that file.
So far I created a name.json file. But I cannot find a solution on how to create one on the desktop.
I could not find a solution on compressing name.json so far.
public MainPage()
{
this.InitializeComponent();
createJson();
UnzipFile();
}
public async void createJson()
{
string text = "This text";
string json = JsonConvert.SerializeObject(text);
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("name.json");
await FileIO.WriteTextAsync(file, json);
}
public async void UnzipFile()
{
var localFolder = ApplicationData.Current.LocalFolder;
var archive = await localFolder.GetFileAsync("Testfile.zip");
ZipFile.ExtractToDirectory(archive.Path, localFolder.Path);
}

Working with .zip files is possible using System.IO.Compression.ZipArchive and related classes. You are already using ZipFile.ExtractToDirectory which is the right approach here. However, the issue you are facing is rather related to permissions.
UWP apps are sandboxed which means they are not allowed to touch any filesystem location by default. This is to ensure better security and easier uninstallation of apps. This however means you cannot easily create a file on the user's desktop, as your app does not have access there. The only folders your app can freely access are those accessible by ApplicationData.Current and then those it declares access to in application manifest. You can also declare broad filesystem access here to get access to all locations on the PC.
To further complicate this, there are two types of I/O APIs in UWP. The modern StorageFile API which is async enabled, but tad slower, and the classic file I/O APIs in C# which includes ZipFile and ZipArchive. The main disadvantage of the classic APIs is that they always have access only to application folders and you can never access any other system paths, even if you declare broad filesystem access.
However, even without declaring broad filesystem access capability you can manually get access to the folder/file of user's choosing using FolderPicker, FileOpenPicker and FileSavePicker. Using these you can let the user choose the destination where you will save the file or open a file.
Finally - to circumvent the limitation of not being able to use the classic file I/O APIs, you can first unzip the .zip file in a temporary folder inside ApplicationData.Current.LocalFolder and then use the StorageFile.MoveAndReplaceAsync(IStorageFile) or StorageFile.MoveAsync(IStorageFolder) method to move the files to the location the user has chosen using FileSavePicker.
For further info you can check out this blog post with a tutorial on using .zip in UWP.

Related

Is there anyway that allows me to use System.IO in a UWP application

I'm currently working on a UWP application and one of the things I need to do is write bytes to files using the System.IO class. This is because one of the libraries my application depends on uses the System.IO.File.WriteAllBytes() method.
Using the StorageFolder class isn't quite something that I'm looking to do since a couple of the projects I am making (some are not UWP, some are CLIs) depend on this library I have written and the StorageFolder is only available in UWP.
I've added broadFileSystemAccess as one of the capabilities of my UWP application. However, after reading the documentation carefully I found out that this capability only affects the StorageFolder class and not the System.IO class. In addition, I'm the only person who will be using this application, so I thought that enabling the file access rights in the System Settings (as shown in the image provided) would help. However that did not help at all
Here is the exception message that I am getting
"Access to the path 'C:\\proj\\proj_tig\\Project 112\\bin\\x64\\Debug\\AppX\\Dependencies.zip' is denied."
Is there any possible way to use the System.IO.File.WriteAllBytes() method at all in UWP?
Is there anyway that allows me to use System.IO in a UWP application
For checking the path, and it looks a apps installation folder, unfortunately, the installation folder is read-only, we can't write data into. we suggest you use app' local folder that with full permission, and you could use System.IO namespace to access. For more detail please refer this document.

How to write a list of classes to a file in UWP C#

I have attempted to use the methods on this thread by deadlydog:
How to quickly save/load class instance to file
This works in a console application for writing a list of classes to a file. However when I try migrating it to a UWP project I always get "Access to the path ..... is denied"
According to this thread: broadFileSystemAccess UWP
I am meant to add broadFileSystemAccess capabilities, which I have done.
However, it also says that I need to use StorageFile APIs, not just classic System.IO API.
I believe the current writing method I am using is utilizing the System.IO API but I do not know how to achieve the same goal using StorageFile APIs.
Could someone please point me in the correct direction?
UWP apps are only allowed to write to a small subset of folders on your machine. Using broadFileSystemAccess capability might alleviate it, but it's like using a sledgehammer when you need a screwdriver.
Your application to write data it needs to Windows.Store.ApplicationData.LocalFolder.Path. That folder is writable without any special capabilities.
You can use the System.IO API, just be sure your code doesn't try to access anything outside its StorageFolder. You can get the StorageFolder path at runtime with this code
Windows.ApplicationModel.Package.Current.InstalledLocation.Path
There is also documentation for how to handle IO in UWP here: https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files

Does File.Exists not work inside a UWP project?

File.Exists(filePath); works perfectly inside a console application, but when I do the same thing inside uwp it doesn't detect a file.
I have tried to put breakpoints on various methods and stepped into anything that could give me some information about the issue, but I'm getting no information at all no matter what i try.
Code from UWP app:
string path = #"C:\Users\Name\Desktop\image.jpg";
if (File.Exists(path))
{
ProcessFile(path);
}
else if (Directory.Exists(path))
{
ProcessDirectory(path);
}
UWP:
When it hits File.Exists i get a return value of false, and yes i know for a fact the image is where it is.
Console:
When it hits File.Exists i get a return value of true, then goes onto the called method without any issues.
I'm expecting my code to find a File and pass the filePath into my method called "processFile".
Does UWP applications not have access to files outside of its LocalStorage or is it another issue that I'm not seeing?
UWP does not have direct access to files outside of the application folder and application data folder. That are the only two locations accessible via the System.IO APIs.
You can use StorageFile APIs to access more locations if you enable appropriate capabilities - like access to libraries or broadFileSystemAccess or use file/folder pickers. In particular, broadFileSystemAccess allows you to access the whole file system, but your app should have a good reason to do so (otherwise it will not pass the Microsoft Store certification process).
For more info see the Docs.
This is correct. UWP apps are sandboxed and cannot access files outside of LocalStorage in this way.
If you want to open a file on the users file system you have to use FileOpenPicker.PickSingleFileAsync or similar to prompt the user to pick a file, which you'll then be able to work with.
Further reading: Working with Files in UWP applications

C# directory to "play with"

Simple question here. I have a C# program which needs to stores some files onto the hard drive, but I don't need them to be anywhere useful to the end-user, only somewhere that the program can read/write from.
Is there a directory that I can reference programmatically to be my "filespace playground" - that is, that I can read/write freely to and from?
EDIT: Also, if I use a temp directory, how long are the files guaranteed to be there? I don't want the them to disappear while my program is still running!
I would use the Application Data Directory. You can get to it using something like:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
It is the prefered location in windows for application specific data and it is generally hidden from the user unless they would like to go and find it.
You can use the system temporary directory which you can get with:
string tmpDir = System.IO.Path.GetTempPath();
If you want, you can create a subfolder under there. The temp folder is great for files that you don't care about. If you want to keep the files you can use the ApplicationData folder as Tim C and Graham Miller suggested.
I think you want the application data directory:
var appplicationDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
It sounds like you're looking for isolated storage:
Isolated storage is a data storage
mechanism that provides isolation and
safety by defining standardized ways
of associating code with saved data.
Standardization provides other
benefits as well. Administrators can
use tools designed to manipulate
isolated storage to configure file
storage space, set security policies,
and delete unused data. With isolated
storage, your code no longer needs
unique paths to specify safe locations
in the file system, and data is
protected from other applications that
only have isolated storage access.
Hard-coded information that indicates
where an application's storage area is
located is unnecessary.
Use the system Temp directory:
System.IO.Path.Combine( System.IO.Path.GetTempPath(), "your app name" )
The temp directory is automatically cleaned up by the system (usually by hard disk cleanup) so it's usually the best bet for storing random files that are only needed while the app is running and don't need to stick around.
If you need a more permanent solution that is storing user data files, use the AppData folder as suggested by other folks.
C:\TEMP?
seriously, no, there is no build-in sandbox playground. You have to chose/create a directory which fits your needs.
This is normally the AppData folder. It's in a user specific directory tree, but not in a place that normal users go.
Usually, app-specific data is stored in locations specified by of the SpecialFolder enumeration. You can use ApplicationData for per-use app-specific data, or LocalApplicationData for per-computer variant:
var playground = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData));
Isolated Storage may work for you.
http://msdn.microsoft.com/en-us/library/bdts8hk0%28VS.95%29.aspx
or,
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/53def6a4-076c-43f8-86e9-e223d99396a0
The base class library offers you a class to manage a set of temporary files. It is well hidden in the System.CodeDom namespace but nonetheless it can be useful in other contexts as well:
TempFileCollection
The following sample shows how the TempFileCollection class can be used.
using System;
using System.CodeDom.Compiler;
using System.IO;
class Program
{
static void Main(string[] args)
{
TempFileCollection tfc = new TempFileCollection(Path.GetTempPath());
// add a temporary text file
string filename1 = tfc.AddExtension("txt");
// add another file with a fully specified name
// this file will not automatically be deleted
string filename2 = Path.Combine(tfc.TempDir, "mycustomfile.txt");
tfc.AddFile(filename2, true);
Console.WriteLine(tfc.Count);
// Create and use the test files.
File.WriteAllText(filename1, "Hello World.");
File.WriteAllText(filename2, "Hello again.");
tfc.Delete();
}
}

Write to a File in Monotouch

How would I create and write to a file in a Monotouch iPhone app?
The file should persist between application launches, so I guess it has to be placed somewhere in the App bundle ( documents or resources?).
[Note: My response is pretty thorough because I don't know your level of understanding regarding app bundles or the structure of your iPhone app's sandboxed little world - apologies if I cover things you already know - I prefer to write a little too much than too little, and to add a bit of the why when discussing the how...]
You have a few options (of course). I'm assuming you're already familiar with .Net to some extent and that your question is more about how to do this the iPhone Way.
Every iPhone app (and you'll see the same thing for apps on OS X) is a "bundle" which isn't an executable in the traditional sense, but actually a folder hierarchy inside of which your app binary lives (along with resources, settings, etc.).
Because of how uber-sandboxed iPhone apps are, you don't have access to the shared folders you'd usually be able to use when doing desktop development (having, for example, a common Documents folder that lives under a user's home folder to which applications have access).
Instead, your app has its own folder hierarchy that's like its own personal set of the folders that would typically be shared across apps.
The easiest way to see what your app's folder structure looks like on the phone is to look at the folder the iPhone simulator uses for app installs, settings, blah blah blah. On my machine (I don't recall if this is configurable, but it's probably the same on your system), you can get to the folder by this path:
~/Library/Application Support/iPhone Simulator
Inside of that, there's a User/Applications folder that contains the apps you've installed to the simulator. Drill down into any one of those folders, and you can see the folder structure your app will have access to on the phone.
For storing files that you'd like persisted across app sessions, your app's Documents folder is the spot. It's not your only choice for creating files, but it's the right choice for this job. In addition to your files being properly stored, keeping them in the Documents folder will also get them backed up by iTunes when the user syncs.
With MonoTouch, you can get your app's Documents folder path with Environment.GetFolderPath(Environment.SpecialFolder.Personal);
If you'd like to test it out, this is some extremely simple code that'll write a file called "out.txt" to your app's Documents folder. This code also reads the contents of the file to show it was created - for further verification, go to the simulator's Applications folder, sort the app folders by the date they were modified, drill down into the most recently modified, and look inside its Documents folder - you'll find "out.txt" (you can't find your app's folder by name because, when your app is installed, it gets stuffed inside a folder with a name like "2B3CA854-FADB-4DDC-9732-0E61B3DD8D8C" - sorting the folders by the date they were modified will point you to the most recently modified app, which, in this case, is whatever app contains the following code):
// For this to function, don't forget "using System.IO;"
// If you're just playing around with this to see it work, place it inside
// your AppDelegate's "FinishedLaunching" method in main.cs
string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "out.txt");
// File.WriteAllText will create a file and then write text to it. If the
// file already exists, File.WriteAllText will overwrite it.
File.WriteAllText(filePath, "Howdy, world.");
// Now we prove it worked by reading the contents of the file and then
// printing them to the console...
string text = File.ReadAllText(filePath);
Console.WriteLine(text);
So, the only thing here that's really iPhone-specific is knowing that "Environment.SpecialFolder.Personal" maps to your app's Documents folder. Beyond that, it's .Net as usual.
And, again, this was probably overkill, but I wanted to answer sufficiently thoroughly for everybody who sees it.
The following How To from the Xamarin.iOS guide site has a few pointers to where to store your files:
http://docs.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
You can do something like this at runtime:
using (StreamWriter writer = new StreamWriter (Path.Combine (Environment.SpecialFolders.Documents, "yourfilename.ext"))) { }
and that will create the file. To open it for reading, use the same Path.Combine() but with StreamReader.

Categories

Resources