简体   繁体   中英

Locating data files used by a C# program in a common location regardless of runtime context?

AC# executable can reside in 3 different locations during it's life cycle when going from development to end user distribution:

  • {project directory}\\bin\\Debug
  • {project directory}\\bin\\Release
  • {end user install directory}

My app has a web page and some ancillary files that I like to put in a sub-directory called \\video , right beneath the directory that contains the executable. What I really don't want to do is copy the sub-directory around between the 3 directories listed above. In other words, obviously I don't want to end up with:

  • {project directory}\\bin\\Debug\\video
  • {project directory}\\bin\\Release\\video
  • {end user install directory}\\video

with the inherent re-copying every time the file in \\video change.

What is a convenient overall strategy for keeping data files distributed with an application in a centralized directory, a directory that will be added to the setup program when the application is distributed? I'm hoping that I don't have to add build tasks to copy over the data files to the \\Debug and \\Release sub-directories every time the application is run in the Visual Studio 2012 IDE. That leads to potential errors if I forget to copy a file during the build tasks and can create a mess if there a lot of files, especially large ones.

Is there a way to build the top level data path that detects conveniently each of the 3 different runtime contexts? I don't mind if I have to wrap all relative data paths with a method that canonicalizes the data path when a relative path is passed to another method. Here's an example using a fictitious method named fixRelPath() that would expand a relative path properly before passing it to a sample method named openFile() :

openFile(fixRelPath(".\\video\\temp.html"));

You should put your files in the AppData directory.

Per User:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Shared:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM