Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅从路径字符串中获取文件名部分?_C# - Fatal编程技术网

C# 如何仅从路径字符串中获取文件名部分?

C# 如何仅从路径字符串中获取文件名部分?,c#,C#,可能重复: 例如,如果我有: d:\test\text.txt或d:\test\test5\test.bmp 例如,我希望string t中的字符串只有text.txt 或者t将是test.bmp 因此t将只包含路径字符串中的文件名部分 守则: public WmvAdapter(string file, string outFolder) { path_exe = path_exe = Path.GetDirectoryName(Application.LocalUserAppDat

可能重复:

例如,如果我有:

d:\test\text.txt
d:\test\test5\test.bmp

例如,我希望
string t
中的字符串只有
text.txt
或者
t
将是
test.bmp

因此
t
将只包含路径字符串中的文件名部分

守则:

public WmvAdapter(string file, string outFolder)
{
    path_exe = path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
    histograms_dat_fileName = "histogramValues.dat";
    histograms_dat_filenameRGB = "histogramValuesRGB.dat";
    histograms_dat_fileName_Directory = path_exe + "\\" + "dat file";
如果文件是例如
d:\text\text.txt
所以

将是:

histograms_dat_fileName_Directory = path_exe + "\\" + "dat file" + "\\" + "text.txt";
因此,当我稍后创建文件时,它将位于目录中,例如:

c:\myuser\appdata\local\dat file\text.txt\
text.txt
是一个路径\文件夹,不是一个文本文件,我希望它被创建为路径

今天的路径是例如:
c:\myuser\appdata\local\dat file

但我想为每个文件名创建另一个路径。

使用:

Path.GetFileName(@“d:\test\text.txt”)


返回
“text.txt”
使用
Path.GetFileName
Path.Combine

string path = Path.Combine(path_exe, "dat file", Path.GetFileName(file));

您至少尝试过谷歌搜索它吗?为什么不使用
System.IO.Path.Combine()
,这比手动组合字符串容易得多。可能的重复:。请在发布新问题之前搜索SO。
string path = Path.Combine(path_exe, "dat file", Path.GetFileName(file));
string fileName = Path.GetFileName("d:\\test\\test5\\test.bmp");
// fileName now contains "test.bmp"