123456
123456

Reputation: 3

How to declare a relative path in C#

I have this solution in visual studio

MicrosoftVisualStudioSolution //being click to open solution
MainfolderSolution
-Model
---Sample.model
-Scripts
---Index.js
-Contents
---text.json

Code from my Sample.model

    {
        try
        {
            string temp = Path.Combine(Directory.GetCurrentDirectory(), @"..\Contents\text.json"); 
            string Data = File.ReadAllText(temp);
        }
        catch
        {
            return null;
        }
    } 

My problem is that it can't find text.json

Upvotes: 0

Views: 1547

Answers (2)

SRoy
SRoy

Reputation: 997

For an example let's see the below code to point crystal report template position string crysRTFReport = Server.MapPath(@"~\Reports\rptRTF_19.rpt");

Now if the file path is like this "C:\Crystal\BankAcc\ERP\Reports\rptRTF_19.rpt" and by mistake if you miss something then it will throw exception and to avoid the full string deceleration and safe typing we use @ before and the string folder\filename.filetype.

Upvotes: 0

ramiramilu
ramiramilu

Reputation: 17182

use -

var path = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/test.json");

Upvotes: 2

Related Questions