I'm trying to load a file with pack://application: The file is situated in the root of my project but I keep getting a null reference error. However When I do an absolute reference it finds the file and loads just fine. What am I missing here?
This doesn't work
var txt = Application.GetContentStream(new Uri(@"pack://application:,,,/Layout.xml"));
string full = new StreamReader(txt.Stream).ReadToEnd();
or any variation with Pack://Application,,,/
This works, but I don't want to use it and seems bad practice anyway
var path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));
var txt = path + @"Layout.xml";
string full = new StreamReader(txt).ReadToEnd();
First, ensure that the file is definitely copied into your output
./bin/
directory on compile:This worked perfectly for me in my WPF application:
If you want to read it as binary (e.g. read an image file), you'll need this helper function. You probably won't need this, as you're reading an
.xml
file.For more, see Microsoft on Pack URIs in WPF.