Use GetFileName to extract file name from path. Like below
string[] s = Directory.GetFiles(@"c:\\test");
foreach (string filename in s)
{
string fname = Path.GetFileName(filename);
}
Frage
i want to store the name of all file present in c:\test\
in a string array s[]
let there are files name a1.txt , a2.txt , a3.txt ...
in c:\test
i want to store in s[0]= a1.txt s[1]= a2.txt
and like that
i have used the code
s = Directory.GetFiles(@"c:\\test");
but it makes s[0]= c:\test\a1.txt
i dont want c:\test
, i only want a1.txt
so is there any method to store only the file name but not the path of the file
i would also like to know if there is any method to remove some characters from each string of a string array
like cutting 5 characters from the beginning of each string of a string array this may also solve my problem.
Lösung
Use GetFileName to extract file name from path. Like below
string[] s = Directory.GetFiles(@"c:\\test");
foreach (string filename in s)
{
string fname = Path.GetFileName(filename);
}
Andere Tipps
maybee duplicate from hare.. any way @Vasea gives us :
Directory.GetFiles(@"c:\test", "*", SearchOption.TopDirectoryOnly).Select(f => Path.GetFileName(f));