Cross-Platform File Name Handling in .Net Core

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



Leave a reply



Submit