Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the folder of a file.

Tags:

c#

I have a folder fuill of images and have the image name and the full file path stored in an array in my program. Is it possible to get just the folder and the filename from the filepath.

So If I have a filepath of

C:\Users\Ryan\Documents\myImage.jpg

I need to get

Documents\myImage.jpg
like image 811
xiimoss Avatar asked Nov 20 '15 10:11

xiimoss


People also ask

How do I find the directory of a file in Windows?

Search File Explorer: Open File Explorer from the taskbar or right-click on the Start menu, choose File Explorer and then select a location from the left pane to search or browse. For example, select This PC to look in all devices and drives on your computer, or select Documents to look only for files stored there.

How do I get the directory of a file in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

How do I find the directory of a file in Linux?

Firstly, we use the dirname command to find the directory in which a file is located. Then we change the directory using the cd command. Next, we print the current working directory using the pwd command. Here, we have applied the -P option to show the physical location instead of the symbolic link.

Is there a way to print a list of files in a folder?

Select all the files, press and hold the shift key, then right-click and select Copy as path. This copies the list of file names to the clipboard. Paste the results into any document such as a txt or doc file & print that. Then open notepad, open tempfilename, and print it from there.


1 Answers

Use this code:

FileInfo f = new FileInfo(@"C:\Users\Ryan\Documents\myImage.jpg");
string result = Path.Combine(f.Directory.Name, f.Name);
like image 61
Fᴀʀʜᴀɴ Aɴᴀᴍ Avatar answered Nov 09 '22 14:11

Fᴀʀʜᴀɴ Aɴᴀᴍ