Search code examples
c#packagedata-files

Accessing a data file from a packaged and installed application


The first time my application runs I want to create a folder and copy CI02Data.txt, which is stored in the release folder, into the new folder. My program works fine in development, but when I package it and install from an appxbundle file, CI02Data.txt is not found. What is the correct path to access CI02Data.txt?

if (!Directory.Exists(@"c:\Plan of Study\Data")) Directory.CreateDirectory(@"c:\Plan of Study\Data");

if (!File.Exists(@"c:\Plan of Study\Data\CI02Data.txt")) File.Copy("CI02Data.txt", @"c:\Plan of Study\Data\CI02Data.txt");


Solution

  • //Get location where assembly is executing
    
    string result = Assembly.GetExecutingAssembly().Location; 
    
    //Construct a path for the new file
     
    int index = result.LastIndexOf("\\"); 
    string dataPathCI04 = $"{result.Substring(0,index)}\\CI04Data.txt";
    
    //Copy file to C drive
    
    if (!File.Exists(@"c:\Plan of Study\Data\CI04Data.txt"))
       File.Copy(dataPathCI04, @"c:\Plan of Study\Data\CI04Data.txt");