2

I have a WCF service that reads a file upon creation.

I added the file to my project, set it's Buid Action to Content and Copy to Output Directory to Copy always, and used the following to get the file path:

private const string MEDIA_PLAYER_FILENAME = "mediaPlayers.xml";
var mediaPlayerFilePath = Path.Combine(Environment.CurrentDirectory, MEDIA_PLAYER_FILENAME);

This works well when I self-host it In a Console app, but when it is being hosted in Windows Service, it fails because it tries to read it from C:\Windows\system32\mediaPlayers.xml

I guess Environment.CurrentDirectory is not the one I should use. What should it be? or is there another approch to do this?

Timeout
  • 7,489
  • 24
  • 37
Omri Btian
  • 6,404
  • 3
  • 37
  • 63

1 Answers1

3

What you want is the path of the executing assembly. The CurrentDirectory is not the same thing.

See this post for getting that information:

Getting the path of the current assembly

Re-post:

(new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

Or

Assembly.GetExecutingAssembly().Location
Community
  • 1
  • 1
Timeout
  • 7,489
  • 24
  • 37