introduce
Let us try to understand C# program to get file name from absolute path. We will see the usage of GetFileName method under File class which is used to get the file name and another method GetFileNameWithoutExtension which will return the file name of the specified path string without extension. To get a relative path from two absolute paths, we will use the Uri class in C#. The Uri class provides several methods for working with URIs, including MakeRelativeUri, which we'll use in our code.
A file is a location used in C# to store some information. A collection of files is called a directory. In C#, files belong to the System.IO namespace, which also includes other classes for performing operations on files. Path is another class under the same namespace. There are two types of paths, absolute paths and relative paths. An absolute path includes all the information needed to locate a file or directory on your system. An example of an absolute path is C:\Program Files\Google Chrome\filename.exe.
The relative path tells us the path of the file relative to the current directory the user is working in. Consider the similar example mentioned above, if the main executable is located in C:\Program Files, the relative path to filename.exe is Google Chrome\filename.exe.
GetFileName method
This is a method under the file class in C#, used to get the file name from the absolute path. path.GetFileName is used to perform this operation. It takes an absolute path as input and returns the filename and extension of the specified path. The absolute path used as input is in string form.
grammar
public static string GetFileName(string path)
The path here is the string we need to get the file name from.
In the above example, GetFileName is a method of the path class. This method returns the characters after the last directory character separator in the pathname. If it does not exist, then it will return a NULL value. It also returns a NULL value if the last character of the path is a directory or volume separator.
Similarly, if the path is null, then it will return a NULL value.
If the file name is "C:\mydir\filename.exe", then the output result is filename.exealgorithm
The algorithm below will give the step-by-step process of getting the file name using the File.GetFileName() method.
For example, if we need to find the name of a file, we can use the following algorithm to understand:
Step 1 − Create a string to store the name of the file path. Remember that this is an absolute path.
Step 2 − Create an initially empty string file name.
Step 3 −We will store the output of the GetFileName method in the string file name.
Step 4 − Since the file name in the above code is "myfile", the output will be the same.
Step 5 − If the path is null in any case, the returned output will also be NULL.
Step 6 − By using the GetFileName method, we can return the name of the file from the absolute path.
Example
using System; using System.IO; using System.Text; class FileName { static void Main(string[] args) { //declaring the string path. string path = "E://Jinku Hu//Starting Over//Csharp//myfile.md"; //declaring the string filename. string filename = null; //we will be storing the value in the filename variable. filename = Path.GetFileName(path); Console.WriteLine(filename); Console.ReadLine(); } }
Output
myfile.md
time complexity
In the above mentioned code, we can observe that there are no loops or nested loops inside the main function, so the time complexity is simply O(1).
GetFileNameWithoutExtension method
The method explained before is to use the GetFileName method to return the name of the file. Now we will see how to return the file name without extension. Assuming the name of the path is "C:\Program Files\Google Chrome\filename.exe", then the returned output is "filename". It accepts absolute paths as input and returns filenames as output. The absolute path entered is in the form of a string. Syntax - "public static string GetFileName(string path)", where path is the string we want to get the file name without extension.
In the above example, GetFileName is a method of the path class. This method will return the characters after the last directory character separator in the pathname, without extension. If it does not exist, then it will return a NULL value. It also returns a NULL value if the last character of the path is a directory or volume separator.
算法
在此算法中,我们将了解使用 GetFileNameWithoutExtension 方法返回不带扩展名的文件名的分步方法。
第 1 步 − 创建一个字符串来存储绝对路径的名称。
步骤2 − 创建另一个名为filename的字符串,该字符串将作为输出返回。
第三步 − 字符串变量filename将以null作为其初始值。
第四步 − 使用GetFileNameWithoutExtension方法生成的输出将存储在一个字符串filename中。
步骤 5 − 如果文件为空,则输出也将相同。
第 6 步 − 因此我们可以使用 GetFileNameWithoutExtension 方法返回不带扩展名的文件名。
示例
using System; using System.IO; using System.Text; class FileName { static void Main(string[] args) { //declare the string path first. string path = "C:\Program Files\Google Chrome\filename.exe"; //declare the string filename and store null initially. string filename = null; filename = Path.GetFileNameWithoutExtension(path); //store the answer in the variable filename. Console.WriteLine(filename); Console.ReadLine(); } }
输出
filename.exe
时间复杂度
在上述代码中,我们可以观察到只使用了一种直接生成输出并将其存储在相应变量中的方法。没有使用任何循环或任何类型的复杂递归函数。这仅仅构成了时间复杂度为O(1)。
结论
在本文中,我们广泛讨论了使用GetFileName方法从绝对路径中获取带有扩展名的文件名,以及使用GetFileNameWithoutExtension方法获取不带扩展名的文件名的C#程序。
我们希望本文能够帮助您增强有关 C# 的知识。
The above is the detailed content of C# program to get file name from absolute path. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
