C
C#15mo ago
Kiel

Directory.CreateDirectory() for a file path while ignoring the filename

I have a file with full path /path/to/some/very/long/file.txt. I would like to be able to use Directory.CreateDirectory(path) to create all folders and subfolders on the way to that file, but in practice, it ends up creating a file.txt folder at the end. Is there a more elegant solution to this other than splitting my path string on / and reforming it minus the last piece?
3 Replies
Angius
Angius15mo ago
Yeah, split and join will do the trick I wonder if System.Path also has some ways to strip the file name Probably A quick and dirty solution would be
string.Join('/', path.Split('/')[..^2]
string.Join('/', path.Split('/')[..^2]
though
Jimmacle
Jimmacle15mo ago
you could also new FileInfo(filePath).DirectoryName i'm not sure if that works if the file doesn't exist though
Kiel
Kiel15mo ago
Actually, I found Path.GetDirectoryName("/very/long/path/to/some/file.txt"), which returns /very/long/path/to/some Was looking in the wrong place I guess 😄
Want results from more Discord servers?
Add your server
More Posts
❔ Passing data from a web page to a ""singleton"" C# desktop applicationSo, let's say I have a web page that has a button that opens my C# application (through a url protocBlazor doesn't add CSS Isolation file ✅Hey guys, I was tried to add a CSS Isolation file in my Blazor aplication (I'm using .NET 7), but th❔ Unit Testing dynamic dataI have a ton of unit tests that look like the following: ```csharp using Dapper; using FluentAsser❔ trying to get published events in one project to the otherI have a Solution with two projects 1. Solution 1a. Project ( winUI3) 1b. Project.core 1a referIs there a way to return or access a class object created inside a public partial class (form) fromI have a process that allows expenses to be added from a form, and a separate process to add an asse❔ Csharp context menu strip change colorHow i can change the color of the icon list of the context menu strip?❔ Anyone got any advice on how to handle currencies in ecommerce/hotel booking?I don't know if it's as straight-forward as it seems, but I'd appreciate any advice from people who'Big problem with this methodThis method is supposed to integrate an equation for velocity and push all the points found by subst❔ Dependency injection & inheritanceThere are 2 classes - tours and tour package. I'm planning to use dependency injection on an instanc❔ i cant return object in JSON correctly in asp.net core web appi cant serialize object correctly, so im creating asp.net core web app, and i have model structured