1

I have a question regarding including a file after it has been copied to the program debug directory.

I have a WPF data grid and in this data grid I display a list of staff with the columns name, age and photo. I can double click a row to open up a new window to update the details and this includes the photo.

In my project directory, I have these folders which I have included in my project. The default folder consists of .jpg files of default staff member photos and the userData folder will be used to store user uploaded photos.

In my .csproj file I have included this:

<ItemGroup>
  <Content Include="img\**">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

This ensures that the img folder gets copied to the output directory upon build. I have also added a .txt file in the userData folder as otherwise it does not copy the folder.

When the user uploads the photo and hits save in my program, it does two things:

  1. Copies the file from source to the output directory of relative path img\userData\filename.jpg
  2. Updates the SQLite record for that staff member's photo path, e.g. From img\default\staffmember1default.jpg to img\userData\newstaffmember1.jpg

My program currently does not update the data grid to reflect changes so upon closing and running the program, it displays a blank photo for the staff member that had the photo change. However, if I was to copy that photo into the included img\userData directory in my project and reload the program it works.

Is there a way to get around this and allow it to display the new photo from the userData directory? Is this whole method suitable or is there a better way of tackling this whole process?

  • Be aware that your application may not have write permission in its installation directory. Better save image files in a subfolder in ApplicationData: https://stackoverflow.com/q/9709269/1136211 – Clemens Apr 17 '19 at 15:37
  • @Clemens Thanks for pointing me in a direction! I managed to create a solution for my problem with your link. – Allan Vargas Apr 17 '19 at 16:08

1 Answers1

1

After looking over the link @Clemens this is the solution I have come up with which seems to do the trick.

private void uploadButton_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();

    if(openFileDialog.ShowDialog() == true)
    {
        string filepath = openFileDialog.FileName;
        string filename = openFileDialog.SafeFileName;

        string local_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        string parent_path = local_path + @"\ProjectName";
        string child_path = parent_path + @"\user";

        if(!System.IO.Directory.Exists(parent_path))
        {
            System.IO.Directory.CreateDirectory(parent_path);
        }

        if(!System.IO.Directory.Exists(child_path))
        {
            System.IO.Directory.CreateDirectory(child_path);
        }

        string target_path = child_path + @"\" + filename;
        File.Copy(filepath, target_path);
        photoBox.Source = new BitmapImage(new Uri(target_path));
        staff.photo_path = target_path;
    }
}

So instead of copying the file to the output directory of the program I decided to do it in the local AppData. The file path also gets stored in the SQLite database and this finds the image fine upon load.