Cross-platform file name handling in .NET Core
Windows using Backslash. Linux using Slash. Path.Combine set the right symbol :
Path.Combine Method - MSDN
.NET: How to set extended file attributes in a cross-platform way?
There is no API (yet). Here's my proposal to add it: https://github.com/dotnet/runtime/issues/49604
Deleting files in a cross platform compatible manner in .NET Core
I just tested this trivial app on Ubuntu 16.04 and it worked.
using System;
using System.IO;
namespace gbsills
{
class Program
{
static void Main(string[] args)
{
File.Delete("file.txt");
}
}
}
Of course, you'll need to make sure you use the correct file path separators and such if you want to be cross-platform.
How to store file paths in a cross platform way in a .json configuration file in ASP.NET Core?
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);
}
Windows vs. unix path on transfer data in .NET
To be OS independent, I would save the relative path as an array in your JSON by using the Path.DirectorySeparatorChar property:
// Example: notepad.exe relative to C:\
var pathParts = Path.GetRelativePath(@"C:\", Path.GetDirectoryName(@"C:\Windows\System32\notepad.exe"))
.Split(Path.DirectorySeparatorChar);
With Path.Combine you can restore your OS dependent part:
var restoredPath = Path.Combine(pathParts);
Related Topics
Json.Net Serialization of Type With Polymorphic Child Object
How to Deserialize an Array of Values With a Fixed Schema to a Strongly Typed Data Class
Login Failed For User 'Iis Apppool\Asp.Net V4.0'
Is Task.Result the Same as .Getawaiter.Getresult()
How to Create Json String in C#
Why Can't I Define a Default Constructor For a Struct in .Net
Comparing Object Properties in C#
Convert Datatable to Json in C#
How to Create Linq Expression Tree to Select an Anonymous Type
Get String Between Two Strings in a String
Ipc Mechanisms in C# - Usage and Best Practices
Splitting a String into Chunks of a Certain Size
Get a Windows Forms Control by Name in C#
What's the Main Difference Between Int.Parse() and Convert.Toint32
Is Datetime.Now the Best Way to Measure a Function'S Performance
How to Speed Up the Build and Run Process in Unity for Mobile Devices iOS/Android