If you’ve definitely got an absolute path, use Path.GetDirectoryName(path)
.
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
Note that Path
and FileInfo
are both found in the namespace System.IO
.
More Related Contents:
- Is there a way to check if a file is in use?
- Easiest way to read from and write to files
- Better way to check if a Path is a File or a Directory?
- How to get files in a relative path in C#
- Is there a faster way than this to find all the files in a directory and all sub directories?
- Delete files older than 3 months old in a directory using .NET
- C# Searching for files and folders except in certain folders
- What’s the fastest way to read a text file line-by-line?
- Nested using statements in C#
- How to find the most recent file in a directory using .NET, and without looping?
- How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
- Best way to resolve file path too long exception
- What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?
- C# – Get a list of files excluding those that are hidden
- Retrieving files from directory that contains large amount of files
- Can I simply ‘read’ a file that is in use?
- How to insert characters to a file using C#
- Wait for file to be freed by process
- Getting current directory in .NET web application
- Read random line from a file? c#
- Find all files in a folder
- Remove readonly attribute from directory
- How to get a MemoryStream from a Stream in .NET?
- Renaming a directory in C# [closed]
- Exclude certain file extensions when getting files from a directory
- Given full path, check if path is subdirectory of some other path, or otherwise
- C#: How to open Windows Explorer windows with a number of files selected
- Encrypt SQLite database in C#
- how to handle spaces in file path if the folder contains the space?
- Method to get all files within folder and subfolders that will return a list