Question

I'm trying to write a relative path from a file but the only relative paths I can get are from the running app file.

This is causing problems as NCrunch runs its test from a different directory so its failing all my tests because it cant find the files which are relative to my code file.

It's important that this be a relative path as there are several people working on this project so absolute paths don't work.

Is there any way to make the path relative to the .cs file where the code is written?

until now I've been using

 private static readonly string MyDocumentsRoot =      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public static readonly string ApplicationRoot = MyDocumentsRoot + @"\Visual Studio 2012\Projects\";

but really need it to be a little more relative

Was it helpful?

Solution

The solution is well documented in NCRUNCH documentation:

Implicit File Dependencies

The other solutions, sooner or later, will fail. (I had similar trouble with MSTest, which offers a similar solution).

OTHER TIPS

You can try with this code - based on GetExecutingAssembly method

string path  = System.Reflection.Assembly.GetExecutingAssembly().Location;
string directory= Path.GetDirectoryName( path );

Or

  string path  = System.Reflection.Assembly.GetAssembly(YourClass).Location;

The documentation shows the paths are relative to the project file:

For example, you could specify the following files to include:

SpecialConfiguration\ImportantConfigFile.config
..\CustomBuild\**.*
..\..\TestResources\Spreadsheets\*.xls

This would include an individual file (ImportantConfigFile.config), the entire contents of the ..\CustomBuild directory, all of its subdirectories, and all .xls files within the ..\..\TestResources\Spreadsheets directory.

All file paths must be specified relative to the project file's path on disk. When NCrunch builds a workspace, it will automatically arrange all dependent files so they exist with the same relative paths in their workspace as they did in their original location.

You can use str=Directory.getdirectory which will give you the path of the app (the folders up until Release or Debug (where your app is located) and save it in a string (the method returns a string). After doing that you can use the str.replace(str1,str2) method which locates str1 and replaces it with str2. Just don't forget that \ inside a string is \\ because \ is an escape character. If you need help with escape characters let me know so I can help you with that as well.

I usually place an old style ini-file where my executable resides. Here I store the absolute paths of files and folders required by the application. This also enables me to configure these pathes differently in a production enivronment (on the cutomers site) than on my development environment. Having just a copy of this ini-file in the folder from where NCrunch runs the assemlies would solve the problem.

My ini-file implementation searches all the folders, starting at the folder where the executable resides, towards the root folder, until it finds the ini-file. This enables me to have different executables using a common ini-file. E.g. if a place the ini-file in the bin folder both, the Debug and the Release version will automatically access the same ini-file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top