Skip to main content
Updated answer.
Source Link
Andrew KeepCoding
  • 15.4k
  • 2
  • 25
  • 40

As an alternative, you can create the folders/files when the app runs.

For example:

  1. Create a "Folder 1" on your project.
  2. Inside "Folder 1", create "Subfolder 1".
  3. Inside "Subfolder 1", create a "TextFile1.txt" file.
  4. Set "TextFile1.txt"'s Build Action to Content.
  5. Implement the next code in App.xaml.cs:
public partial class App : Application
{
    private Window _window;

    public App()
    {
        this.InitializeComponent();
    }

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        await CreateAppFolders();
        _window = new MainWindow();
        _window.Activate();
    }

    private static async Task CreateAppFolders()
    {
        StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> temporaryFolderSubFolders = await temporaryFolder.GetFoldersAsync();

        // Returns if "Folder 1" already exists.
        if (temporaryFolderSubFolders.FirstOrDefault(folder => folder.Name == "Folder 1") is StorageFolder folder1)
        {
            return;
        }

        System.Diagnostics.Debug.WriteLine($"Creating 'Folder 1' and its subfolders on {temporaryFolder.Path}");

        folder1 = await temporaryFolder.CreateFolderAsync("Folder 1", CreationCollisionOption.OpenIfExists);

        StorageFolder subFolder1 = await folder1.CreateFolderAsync("SubFolder 1", CreationCollisionOption.OpenIfExists);
        StorageFile textFile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Folder 1/SubFolder 1/TextFile1.txt"));
        await textFile1.CopyAsync(subFolder1, "TextFile1.txt", NameCollisionOption.ReplaceExisting);

        StorageFolder subFolder2 = await folder1.CreateFolderAsync("SubFolder 2", CreationCollisionOption.OpenIfExists);
        StorageFolder subFolder3 = await folder1.CreateFolderAsync("SubFolder 3", CreationCollisionOption.OpenIfExists);
    }
}

As an alternative, you can create the folders/files when the app runs.

For example:

  1. Create a "Folder 1" on your project.
  2. Inside "Folder 1", create "Subfolder 1".
  3. Inside "Subfolder 1", create a "TextFile1.txt" file.
  4. Set "TextFile1.txt"'s Build Action to Content.
  5. Implement the next code in App.xaml.cs:
public partial class App : Application
{
    private Window _window;

    public App()
    {
        this.InitializeComponent();
    }

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        await CreateAppFolders();
        _window = new MainWindow();
        _window.Activate();
    }

    private static async Task CreateAppFolders()
    {
        StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> temporaryFolderSubFolders = await temporaryFolder.GetFoldersAsync();

        // Returns if "Folder 1" already exists.
        if (temporaryFolderSubFolders.FirstOrDefault(folder => folder.Name == "Folder 1") is StorageFolder folder1)
        {
            return;
        }

        folder1 = await temporaryFolder.CreateFolderAsync("Folder 1", CreationCollisionOption.OpenIfExists);

        StorageFolder subFolder1 = await folder1.CreateFolderAsync("SubFolder 1", CreationCollisionOption.OpenIfExists);
        StorageFile textFile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Folder 1/SubFolder 1/TextFile1.txt"));
        await textFile1.CopyAsync(subFolder1, "TextFile1.txt", NameCollisionOption.ReplaceExisting);

        StorageFolder subFolder2 = await folder1.CreateFolderAsync("SubFolder 2", CreationCollisionOption.OpenIfExists);
        StorageFolder subFolder3 = await folder1.CreateFolderAsync("SubFolder 3", CreationCollisionOption.OpenIfExists);
    }
}

As an alternative, you can create the folders/files when the app runs.

For example:

  1. Create a "Folder 1" on your project.
  2. Inside "Folder 1", create "Subfolder 1".
  3. Inside "Subfolder 1", create a "TextFile1.txt" file.
  4. Set "TextFile1.txt"'s Build Action to Content.
  5. Implement the next code in App.xaml.cs:
public partial class App : Application
{
    private Window _window;

    public App()
    {
        this.InitializeComponent();
    }

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        await CreateAppFolders();
        _window = new MainWindow();
        _window.Activate();
    }

    private static async Task CreateAppFolders()
    {
        StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> temporaryFolderSubFolders = await temporaryFolder.GetFoldersAsync();

        // Returns if "Folder 1" already exists.
        if (temporaryFolderSubFolders.FirstOrDefault(folder => folder.Name == "Folder 1") is StorageFolder folder1)
        {
            return;
        }

        System.Diagnostics.Debug.WriteLine($"Creating 'Folder 1' and its subfolders on {temporaryFolder.Path}");

        folder1 = await temporaryFolder.CreateFolderAsync("Folder 1", CreationCollisionOption.OpenIfExists);

        StorageFolder subFolder1 = await folder1.CreateFolderAsync("SubFolder 1", CreationCollisionOption.OpenIfExists);
        StorageFile textFile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Folder 1/SubFolder 1/TextFile1.txt"));
        await textFile1.CopyAsync(subFolder1, "TextFile1.txt", NameCollisionOption.ReplaceExisting);

        StorageFolder subFolder2 = await folder1.CreateFolderAsync("SubFolder 2", CreationCollisionOption.OpenIfExists);
        StorageFolder subFolder3 = await folder1.CreateFolderAsync("SubFolder 3", CreationCollisionOption.OpenIfExists);
    }
}
Source Link
Andrew KeepCoding
  • 15.4k
  • 2
  • 25
  • 40

As an alternative, you can create the folders/files when the app runs.

For example:

  1. Create a "Folder 1" on your project.
  2. Inside "Folder 1", create "Subfolder 1".
  3. Inside "Subfolder 1", create a "TextFile1.txt" file.
  4. Set "TextFile1.txt"'s Build Action to Content.
  5. Implement the next code in App.xaml.cs:
public partial class App : Application
{
    private Window _window;

    public App()
    {
        this.InitializeComponent();
    }

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        await CreateAppFolders();
        _window = new MainWindow();
        _window.Activate();
    }

    private static async Task CreateAppFolders()
    {
        StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
        IReadOnlyList<StorageFolder> temporaryFolderSubFolders = await temporaryFolder.GetFoldersAsync();

        // Returns if "Folder 1" already exists.
        if (temporaryFolderSubFolders.FirstOrDefault(folder => folder.Name == "Folder 1") is StorageFolder folder1)
        {
            return;
        }

        folder1 = await temporaryFolder.CreateFolderAsync("Folder 1", CreationCollisionOption.OpenIfExists);

        StorageFolder subFolder1 = await folder1.CreateFolderAsync("SubFolder 1", CreationCollisionOption.OpenIfExists);
        StorageFile textFile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Folder 1/SubFolder 1/TextFile1.txt"));
        await textFile1.CopyAsync(subFolder1, "TextFile1.txt", NameCollisionOption.ReplaceExisting);

        StorageFolder subFolder2 = await folder1.CreateFolderAsync("SubFolder 2", CreationCollisionOption.OpenIfExists);
        StorageFolder subFolder3 = await folder1.CreateFolderAsync("SubFolder 3", CreationCollisionOption.OpenIfExists);
    }
}