-
Notifications
You must be signed in to change notification settings - Fork 101
Application Folder And Files
Yehuda Kremer edited this page Jan 23, 2022
·
1 revision
One of the strongest points of MSIX is total isolation by virtualize the file system.
After the app is installed, windows creates a virtual folder and files tree (copy of the real folders and files), and the installed app can work (read/write) only with this virtual tree.
After the app is uninstalled, the all virtual tree is deleted.
I created a small app to illustrate those points,
its create new folder 'MyFolder', and create random files in it.

- create msix installer for this app, and install it -> the app show that "MyFolder" not exist yet
- click on "Create My Folder" button, then spam-click the "create random files" button -> the app show the location of the created folder and its content (remember that the folder location is not 'real', you cant manually open the path, only the app can use this virtual location)
- close and reopen the app -> the app show the last created folder and files
- uninstall and reinstall the app - the app show that the folder and its content are deleted (uninstalling deletes the virtual file system)
The app code:
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
String myFolderPath = '${Platform.environment['AppData']}\\MyFolder';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isMyFolderExists = false;
List<String> files = [];
_MyHomePageState() {
checkMyFolder();
}
Future<void> createMyFolder() async {
await Directory(myFolderPath).create();
await checkMyFolder();
}
Future<void> deleteMyFolder() async {
await Directory(myFolderPath).delete(recursive: true);
await checkMyFolder();
}
Future<void> createRandomFileInMyFolder() async {
final String fileName = '${Random().nextInt(100)}';
final File file = File('$myFolderPath\\$fileName.txt');
await file.create();
await checkMyFolder();
}
Future<void> checkMyFolder() async {
bool myFolderExists = await Directory(myFolderPath).exists();
List<String> myFolderFileNames = [];
if (myFolderExists) {
var myFolderEntities = await Directory(myFolderPath).list().toList();
myFolderFileNames = myFolderEntities.map((e) => e.path).toList();
}
setState(() {
isMyFolderExists = myFolderExists;
files = myFolderFileNames;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: createMyFolder,
child: const Text(
'Create My Folder',
),
),
Container(width: 10),
isMyFolderExists
? ElevatedButton(
onPressed: deleteMyFolder,
child: const Text(
'Delete My Folder',
),
)
: const Text(''),
Container(width: 10),
isMyFolderExists
? ElevatedButton(
onPressed: createRandomFileInMyFolder,
child: const Text(
'create random file in my folder',
),
)
: const Text(''),
],
),
Text(
isMyFolderExists
? 'is my folder exists in: $myFolderPath'
: 'my folder not exists in: $myFolderPath',
style: TextStyle(
color: isMyFolderExists ? Colors.green : Colors.red,
),
),
Column(
children: files.map((e) => Text(e)).toList(),
),
],
),
),
);
}
}