PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How to store file paths in a cross platform way in a .json configuration file in ASP.NET Core?

 September 12, 2022     asp.net-core, configuration, cross-platform, directory, path     No comments   

Issue

How to store file paths in a cross platform way in a .json configuration file in ASP.NET Core?

E.g. I have the entry in the configuration:

MyPath: "C:\\Foo\\Bar"

And the path works on Windows, but does not work on Linux. I am aware of the Path.DirectorySeparatorChar, but how could I use it in the .json? Or should I use something different?


Solution

try it:

                    // IWebHostEnvironment _webHost
                    // IConfiguration Configuration
                    var listOfPath = Configuration["my-path"].ToString()
                        .Split("\\", StringSplitOptions.RemoveEmptyEntries);

                    var uploadPath = Path.Combine(_webHost.WebRootPath );
                    foreach (var folders in listOfPath)
                    {
                        uploadPath = Path.Combine(uploadPath, folders);
                    }


Answered By - foad abdollahi
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing