1

I am developing an Universal Windows App on C# in Visual Studio 2017. I am working with xml files for saving data. The problem is that when I try to run the builded app, using the executable file, it just doesn't open. I figured out it might be because of the path declaration of the xml files. That's how I have declared the path to the xml files:

string path = Directory.GetCurrentDirectory() + @"\people2.xml";
XDocument document = XDocument.Load(path);
FileStream fss = new FileStream(path, FileMode.Open, FileAccess.Write);

If I have to change something in the XML file properties please let me know. xml File Properties

zx485
  • 28,498
  • 28
  • 50
  • 59
  • If this is a UWP than you can try `Package.Current.InstalledLocation`. – Martin E Jul 23 '18 at 21:15
  • I used `string path = Package.Current.InstalledLocation + "people2.xml";` but it is not working properly. There is an error: Could not find a part of the path 'D:\Program Files\Visual Studio\source\repos\Roster Random\Roster Random\bin\x86\Debug\AppX\Windows.Storage.StorageFolder\people2.xml'.'. The part with the Windows.Storage.StorageFolder should not be there – Kaloyan Terziev Jul 23 '18 at 21:33

1 Answers1

1

You could use the following call that will return the path of your current assembly:

Path.GetDirectoryName(typeof(<SomeTypeInYourAssembly>).GetTypeInfo().Assembly.Location);

This path would be used by your application to save the XML files as well as loading them.

For instance, here is an example the line being called right in the App's constructor. The location variable points to the following folder:

C:...\X.App\bin\Debug\AppX\entrypoint\

enter image description here

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
  • After including System.Reflections error occurred: 'Assembly' does not contain a definition for 'GetExecutingAssembly' – Kaloyan Terziev Jul 23 '18 at 21:06
  • Right. It changed in UWP. I have adjusted the answer accordingly. – Kzryzstof Jul 23 '18 at 21:16
  • Sorry for my incompetance but what should I write instead of ? – Kaloyan Terziev Jul 23 '18 at 21:27
  • You can write a type of a class that you have your UWP. Let's say you have the class called People, you would then have this statement: Path.GetDirectoryName(typeof(People).GetTypeInfo().Assembly.Location); It may be a good idea get this value once (like in a static ctor) and have it stored close to where it is used. – Kzryzstof Jul 23 '18 at 21:31
  • Sure. I have added a screenshot of the app I am working on in the answer. – Kzryzstof Jul 23 '18 at 22:25
  • Thank you very much for the response but another error showed up: 'Assembly' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'Assembly' could be found (are you missing a using directive or an assembly reference?) That's what I have written: `public sealed partial class MainPage : Page { string location; public MainPage() { location = Path.GetDirectoryName(typeof(MainPage).GetTypeInfo().Assembly.Location); this.InitializeComponent(); } }` – Kaloyan Terziev Jul 23 '18 at 22:54
  • 1
    Which version of the Windows 10 SDK are you targeting? You will find this in the properties page of your project. – Kzryzstof Jul 23 '18 at 23:10
  • 1
    In NuGet, make sure you are using the latest package Microsoft.NETCore.UniversalWindowsPlatform – Kzryzstof Jul 23 '18 at 23:23
  • It worked with a bit of adjustments (I had to delete the last 10 characters from the string in order to get ot of the last folder: entrypoints) But the problem with the app not opening from the executable file persist. Any thoughts on that matter? Thank you very much for the help. Appreciate it! – Kaloyan Terziev Jul 24 '18 at 06:14
  • The project is builded and it runs when I start it using visual studio. However, when I try to run the exe file, nothing happens. That's why I thouht that a change in the declaration of the path of the xml file would fix it but it did not. – Kaloyan Terziev Jul 24 '18 at 12:33