c# 如何获取不带后辍的名称
时间: 2024-03-26 09:41:14 浏览: 9
可以使用Path.GetFileNameWithoutExtension方法来获取不带后缀的文件名。示例代码如下:
```
string fileName = "example.txt";
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine(fileNameWithoutExtension); // 输出 example
```
相关问题
C# 获取不带后缀文件名
在C#中,你可以使用以下代码来获取不带后缀的文件名:
string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
其中,filePath是文件的路径。这个方法会返回不带后缀的文件名。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
C# 根据名称获取后缀
要根据名称获取后缀,可以使用C#中的Path类的GetExtension方法。以下是获取文件名为"example.txt"的后缀的示例代码:
```
string fileName = "example.txt";
string extension = Path.GetExtension(fileName);
```
该代码将返回".txt"字符串作为文件的后缀。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)