Joris Mathijssen
Joris Mathijssen

Reputation: 669

FileNotFoundException when reading file using FileInfo

I'm currently using fileinfo to use a xsd file in a xml validator.

I have 3 projects, a GUI, interface, project. A input project and a testproject.

Inside the input project i have a class that creates a FileInfo Object based on a path to a file that is also inside the same object:

new FileInfo(@"Xsd\Version_813\kvppt-8130.xsd")

This gives me trouble when i'm running my tests and my GUI. For example when i start my GUI, sometimes the code works and there is no problem but other times FileInfo will search the Bin/debug folder from the GUI project and the XSD isnt there.

I know that this is intended but is there a way to make this work for the unittest Project and the GUI project.

I have tried the following:

var executingFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
var path = executingFileInfo.DirectoryName + Path.DirectorySeparatorChar + @"Xsd\Version_813\kvppt-8130.xsd";
Return new FileInfo(path);

But this isn't the solution because it will still pick the startup project.

--- Edit:

After trying some of the suggestions in the comments i found out that my problem is a bit diffrent. I found out that if i press rebuild all before running my application, the files are always copied to their correct folder. It only fails if i just run the application.

Properties of the files are on "Copy always"

Upvotes: 0

Views: 162

Answers (1)

Chuck Savage
Chuck Savage

Reputation: 11955

...sometimes the code works and there is no problem but other times FileInfo will search the Bin/debug folder from the GUI project and the XSD isnt there.

Make sure on the properties of the file (from within Visual Studio) you have "Copy to Output Directory" set to either "copy always" or "copy if newer"

Upvotes: 1

Related Questions