Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local files inside solution folder path issue

I have created a WPF project and added a "ResFiles" folder to my solution. Inside this folder I have put couple of XML files. In code, to access these files I am using relative path as described below.

using (StreamWriter writer = new StreamWriter(@"../../ResFiles/xyz.xml"))
        {
            serializer.Serialize(writer, mydata, ns);
        }

This is working fine. But when I create setup project for my app, the folder structure is changing and the path given wont work. It is throwing file not found exceptions. How to tackle this path issue?

like image 954
Niranjan NT Avatar asked Mar 07 '26 15:03

Niranjan NT


1 Answers

you can try this

 var f = File.ReadAllText(System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + @"/Files/file.txt");

and also you need to change Build Action and Copy to Output Directoryenter image description here

like image 52
Justin CI Avatar answered Mar 09 '26 04:03

Justin CI