Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
i want to make a text box in c# and automatic get the filename from path that i gave.
thanks
Posted
Comments
Kornfeld Eliyahu Peter 20-Dec-15 9:03am    
There is no such thing 'automatic' in our world...Someone, somewhere have to write the code for that - in this case you should be the one...
Stm21 20-Dec-15 9:07am    
can i have some help ?
Kornfeld Eliyahu Peter 20-Dec-15 9:14am    
Right after you show some effort on your part...Try some Google search! You will find that this is not that hard you may think just now...
BillWoodruff 20-Dec-15 9:41am    
Please revise your question so it's clear. If you have a valid FilePath, then the name of the file is included in that Path

If you are getting a filepath of a Directory from the user's entry in a TextBox to use for some purpose, please say so.

Remember, we are not mind-readers.

C#
String path=@"C:\Program Files\Test.txt";
MessageBox.Show(Path.GetFileName(path));

result will be "Test.txt"
 
Share this answer
 
v2
Comments
Stm21 20-Dec-15 10:09am    
with the seccond solution i get one expected error in the end of the line MessageBox.Show(Path.GetFileName(path);
Basmeh Awad 20-Dec-15 10:12am    
one closing bracket was missing. check now
Stm21 20-Dec-15 10:14am    
thank you so much!!!!
Stm21 20-Dec-15 10:15am    
now i have a message box but i want this test.txt to place in the text box
Basmeh Awad 20-Dec-15 10:20am    
textBox1.Text = Path.GetFileName(path);
Try:
C#
string pathToFile = @"D:\Temp\Myfile.txt";
string justTheFileName = Path.GetFileNameWithoutExtension(pathToFile);  // MyFile
string justTheFileNameWithExtension = Path.GetFileName(pathToFile);     // MyFile.txt
string justTheExtension = Path.GetExtension(pathToFile);                // .txt
string justThefolderItsIn = Path.GetDirectoryName(pathToFile);          // D:\Temp
 
Share this answer
 

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