Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How You Will Get The Name Of the FOLDER THAT YOU ARE IN...?

What I have tried:

NOTHING .....DON'T KNOW. I CAN ONLY ANTICIPATE THAT THE CODE MAY INCLUDE SYSTEM.IO NAMESPACE ....
Posted
Updated 31-Jan-21 4:15am
Comments
PIEBALDconsult 31-Jan-21 19:43pm    
Yet be aware that a file may be catalogued in more than one directory.
Member 12712527 1-Feb-21 1:44am    
sure sir

You can't get "the containing folder" from a file, but since you know the path to the file - or you should if you want to open or read it - you can use that path to access the folder name:
C#
string path = @"C:\Working data\files\MyFile.txt";
string filenameOnly = Path.GetFileName(path);
string pathWithoutFilename = Path.GetDirectoryName(path);
string justTheFoldername = pathWithoutFilename.Substring(pathWithoutFilename.LastIndexOf('\\') + 1);
Debug.WriteLine(path);
Debug.WriteLine(filenameOnly);
Debug.WriteLine(pathWithoutFilename);
Debug.WriteLine(justTheFoldername);
Will give you:
C:\Working data\files\MyFile.txt
MyFile.txt
C:\Working data\files
files

If you don't use a path when you access a file, then you need to rethink your whole approach: Where should I store my data?[^] should help.
 
Share this answer
 
v2
Comments
Member 12712527 31-Jan-21 10:23am    
thank you sir .... i will not just copy and paste your code but i will modify the code
to get the work done
Member 12712527 31-Jan-21 10:42am    
sir as in your code if i want to know the name of the 'working data' folder where my file is myfile.txt how ill be able to . the name of the specified folder is in a variable but not showing how to get that name through code
Dave Kreskowiak 31-Jan-21 10:57am    
You seem to be really confused about how things work. A "text file" does not know anything. There's no code in it to run so there's no way it'll do anything.

An application can get the full path to the file, but how you do that depends on how your app interacts with the user and/or the files. For example, an OpenFileDialog will let the user navigate to and pick the file, and the dialog will give you the full path to the file, including the parent folders.

If you're trying to interact with a file using just a filename, you're making a huge mistake. ALWAYS generate and use fully qualified paths to files if you're going to interact with them in any way.
Member 12712527 31-Jan-21 10:51am    
the path is "@datafile/"+ strtxt+"/cc.txt".
but strtxt is entered by user in another form .in the working form this value of strtxt
is missing . how to get the value through code
Dave Kreskowiak 31-Jan-21 11:00am    
If strtxt is empty, you're doing something very wrong in your code, but since we know nothing of how you wrote the code and passing data between forms (which are just classes), it's impossible to tell you what's wrong.
For a Winforms application:
Application.ExecutablePath()

For a Windows service:
System.AppDomain.CurrentDomain.BaseDirectory()
 
Share this answer
 
v2
Comments
Member 12712527 31-Jan-21 9:50am    
sir i want to know the name of the folder that my text file is in
Member 12712527 31-Jan-21 9:52am    
suppose that i am a text file and i want to know the name of my containing folder how could i be able to ...?
RickZeeland 31-Jan-21 10:04am    
We can't tell where you save your text files, only you know :)
Member 12712527 31-Jan-21 10:08am    
yes sir but i am building an application where i will be able to know where my text file is residing under which folder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900