Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

filepath - absolute File path to relative file path

I have a class library project (D:/projectName/SampleClassPrj) and a console application project D:/projectName/ConsolePrj. In class library project, a json file test.json and a class sample.cs (read and deserialise json) both present. d:/projectName/SampleClassPrj/test.json d:/projectName/SampleClassPrj/sample.cs

This works with absolute path like, d:/projectName/SampleClassPrj/test.json

but not working with relative path.

with relative path, by using file not found error as it is trying to find the file in the console (running) application assembly folder. D:/projectName/ConsolePrj/bin/test.json please help to find the solution

question from:https://stackoverflow.com/questions/65931113/absolute-file-path-to-relative-file-path

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • first, check json file property "copy if newer". enter image description here
  • then, use Appdomain.CurrentDomain.BaseDirectory
string result = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ConnectionString.txt"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...