Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a solution with two projects as project1 and project2. Default project is project1. Inside project1 i have write some xml files located in a directory inside Release\App_Data\test.xml
I can easily access the file inside project1 by using "Environment.CurrentDirectory + @"\App_Data\test.xml"

Now i need to access the above file (updated) inside project2. Can anyone suggest me how to achieve this?
The xml file is updated regularly from project1, so i need to access the updated copy always.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Aug-15 23:01pm    
Runtime or build time? Does this file goes to output path or not?
—SA
DamithSL 5-Aug-15 23:36pm    
is there any reason to use xml file instead of Database table?
Ajay_Saini 6-Aug-15 0:38am    
Yes... my client dont want to store data into database. So i am using XML file to store data.

You already moved your development into considerable abuse. No, you cannot access the file using Environment.Current directory. You don't really control this directory, your user does. The user can start an application from absolutely any directory, and then it will be the current directory at the moment of the application start. Your file will be very well lost. Just forget it.

But generally, you biggest problem is poor understanding the difference between runtime and design time. During runtime, there is Environment.Current directory but there is no such concept as "project"; there are assemblies. During design/build time, there is no a current directory, and there are projects.

Instead of doing nasty abuse, you can do million of other valid things. You could store the the file in a resource of one assembly. Another assembly could reference another one, which could provide access to either XML string, or stream, of just XML data via some API. This way, you could share this data in both assemblies. If the XML can be readonly, you can put both assemblies in the same executable directory (not current directory!) and share it there (please see my past answer: How to find my programs directory[^]). Better yet, you could abstract out the data from XML file. You could use Data Contract to share some object graph read from XML, which could be done by only one of the assemblies. Please see: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^]. This way, XML would be just the implementation detail, and you would not work with XML directly. And so on.

Just to give you some conceptual understanding: please see:
https://en.wikipedia.org/wiki/Single_Source_of_Truth[^],
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA
 
Share this answer
 
C#
string Path = Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName+@"\Project2\App_Data\test.xml;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900