I am using this code
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Gallery/GalleryImage/" + v));
foreach (string item in filePaths)
{
Response.Write(item);
}
Problem in this code i am getting file name with full path like this
C:\Users\AGENTJ.AGENTJ-PC\Documents\Visual Studio 2010\WebSites\mfaridalam\Gallery\GalleryImage\c050\DSC_0865.JPG
I just want file which is "DSC_0865.JPG"
You can use the Path.GetFileName method to get the file name (and extension) of the specified path, without the directory:
foreach (string item in filePaths)
{
string filename = Path.GetFileName(item);
Response.Write(filename);
}
Try using Path.GetFileName:
Directory.GetFiles(Server.MapPath("~/Gallery/GalleryImage/" + v))
.Select(Path.GetFileName);
You could use the methods in the Path such as GetFileName if you just want the bit without the path.
In your case something like:
foreach(string item in filePaths)
{
Response.Write(Path.GetFileName(item));
}
This is pretty straight forward:
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Gallery/GalleryImage/" + v));
foreach (string item in filePaths)
{
Response.Write(System.IO.Path.GetFileName(item));
}
Related
I have this method that searches all files and folders in "C:\Sharing".
string[] fileArray = Directory.GetFiles(#"C:\Sharing", "*.*", SearchOption.AllDirectories);
And foreach shows me full path of each file. Great. However, since these are in a directory called "Sharing", I want to check and add files that are like
C:\Sharing\Jerry2022\wedding.jpg (array: 'wedding.jpg', 'Jerry2022')
C:\Sharing\snapshot.jpg (array: 'snapshot.jpg')
C:\Sharing\Newsletter\cover-june.webp (array: 'cover-june.webp', 'Newsletter')
So as you can see, I want to add file and subdirectory name to a string array or List, doesnt matter. Excluding "Sharing".
How can I split the results? I know I can use Substring and LastIndexOf("\") + 1 and separate the ending '' but I'm not sure how to match up the filename with the subdir name too.
Any help is appreciated
You can use DirectoryInfo to get the information you want:
C#:
var directoryInfo = new DirectoryInfo(#"C:\Sharing");
if (directoryInfo.Exists)
{
foreach (var fileInfo in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
{
var fileName = fileInfo.Name;
Console.WriteLine(fileName);
var directoryName = fileInfo.DirectoryName;
// you can use split to get the directory name array
Console.WriteLine(directoryName);
}
}
I found an other way, use Uri for this scenario:
C#:
string[] fileArray = Directory.GetFiles(#"C:\Sharing", "*.*", SearchOption.AllDirectories);
foreach (var s in fileArray)
{
var uri = new Uri(s);
var uriSegments = uri.Segments.ToArray();
}
You will see each part of the full path, but you may need to use .Trim('/') for each part. Then you can use string.Equals to get directories which you want.
You could split the results using Split
But of course you can also work with FileInfo instead
I want to write foreach loop to get all files with specified extention from external txt file. For example I have in file variable:
extensions = "jpg,tif,bmp,png" or
extensions "jpg,tif" and I want to only get this files.
So far I have something like this but I don`t know how to go on.
extensions = Extensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string sourceFile in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(extensions.)))
{
}
I don`t know how to get to every element in 'extensions' array. How can I solved that?
You can use Enumerable.Contains and System.IO.Path.GetExtension:
string[] extensions = {".jpg",".tif",".bmp",".png" };
var files = Directory.EnumerateFiles(SourcePath, "*.*", SearchOption.AllDirectories)
.Where(s => extensions.Contains(Path.GetExtension(s), StringComparer.InvariantCultureIgnoreCase));
string path = AppDomain.CurrentDomain.BaseDirectory;
string[] filePaths = Directory.GetFiles(path, "*.txt");
foreach (string file in filePaths)
{
cboLanden.Items.Add(file);
}
This is my code and it returns the full path, I would like to have only the name, without the path in my combobox.
Use Path.GetFileName() to get file name without path:
string path = AppDomain.CurrentDomain.BaseDirectory;
string[] filePaths = Directory.GetFiles(path, "*.txt");
foreach (string file in filePaths)
{
cboLanden.Items.Add(Path.GetFileName(file));
}
Also consider to use files as data source of your comboBox:
cboLanden.DataSource = Directory.EnumerateFiles(path, "*.txt")
.Select(Path.GetFileName)
.ToList();
Simple just use this
foreach (string file in files)
{
Path.GetFileNameWithoutExtension(file);
}
If there is a file name in files like c:\coolpic.jpg
it will return only coolpic without extension
How to access the files in ListViewControl?
On an initial event, I have a button that lists the necessary files on the lsitView using foreach loop. The control has 2 items: File Name & File Path (subItem)
DirectoryInfo d = new DirectoryInfo(path);
foreach (FileInfo f in d.GetFiles("*.txt", SearchOption.AllDirectories))
{
lstProjectFiles.Items.Add(f.Name).SubItems.Add(f.DirectoryName.ToString());
}
Now, I would like to display each items' full path plus the filename with extensions.
foreach(ListViewItem f in lstView1.Items)
{
Console.Writeline(f.ToString());
}
When I get the filename, the output is like below. There are extra string and not the filename.extension only:
ListViewItem: {sample.txt}
Try something like this, it is using the SubItems Text Property.
foreach (ListViewItem f in lstProjectFiles.Items)
{
Console.WriteLine(f.SubItems[0].Text);
Console.WriteLine(f.SubItems[1].Text);
}
You are only loading the file name into the list box, not the FileInfo.
How about cheating a little and storing the FileInfo in the Tag property?
DirectoryInfo d = new DirectoryInfo(path);
foreach (FileInfo f in d.GetFiles("*.txt", SearchOption.AllDirectories))
{
lstProjectFiles.Items.Add(f.Name).SubItems.Add(f.DirectoryName.ToString()).Tag = f; // Store FileInfo in Tag property
}
...
foreach(ListViewItem f in lstView1.Items)
{
Console.Writeline(((FileInfo)f.SubItems[0].Tag).FullName);
}
Then you can access anything in the FileInfo object later on. Of course you may want to refactor & validate, but this is the gist of one approach you could take.
I am using the below method to get the file names. But it returns the entire path and I don't want to get the entire path. I want only file names, not the entire path.
How can I get that only file names not the entire path
path= c:\docs\doc\backup-23444444.zip
string[] filenames = Directory.GetFiles(targetdirectory,"backup-*.zip");
foreach (string filename in filenames)
{ }
You could use the GetFileName method to extract only the filename without a path:
string filenameWithoutPath = Path.GetFileName(filename);
System.IO.Path is your friend here:
var filenames = from fullFilename
in Directory.EnumerateFiles(targetdirectory,"backup-*.zip")
select Path.GetFileName(fullFilename);
foreach (string filename in filenames)
{
// ...
}
Try GetFileName() method:
Path.GetFileName(filename);
You can use this, it will give you all file's name without Extension
List<string> lstAllFileName = (from itemFile in dir.GetFiles()
select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList();
Linq is good
Directory.GetFiles( dir ).Select( f => Path.GetFileName( f ) ).ToArray();