
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to get the file name in C#
Set the path name in a string −
string myPath = "D:\
ew\quiz.txt";
Now, use the GetFileName() method to get the name of the file −
Path.GetFileName(myPath)
The following is the complete code −
Example
using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string myPath = "D:\
ew\quiz.txt"; // get extension Console.WriteLine("Extension: "+Path.GetExtension(myPath)); // get path Console.WriteLine("File Path: "+Path.GetFileName(myPath)); } } }
Output
Extension: .txt File Path: D:
ew\quiz.txt
- Related Articles
- C# Program to get the name of the file from the absolute path
- C# program to get the extension of a file in C#
- C# Program to get information about a file
- C program to change the file name using rename() function
- C# Program to get the name of the drive
- C# Program to get the name of root directory
- C# Program to get the last write time of a file
- C# Program to get the last access time of a file
- How to get the file name from the file path in Python?
- Java Program to get name of specified file or directory
- Golang program to get the name of the file from the absolute path
- C# Program to Create File and Write to the File
- How to Get File Owner’s Name in Java?
- Get file extension name in Java
- Java Program to get the name of the parent directory of the file or directory

Advertisements