Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:25+00:00 2026-05-16T22:16:25+00:00

string path = "C:\folder1\folder2\file.txt"; What objects or methods could I use that would give

  • 0
string path = "C:\folder1\folder2\file.txt";

What objects or methods could I use that would give me the result folder2?

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:16:25+00:00Added an answer on May 16, 2026 at 10:16 pm

    I would probably use something like:

    string path = "C:/folder1/folder2/file.txt";
    string lastFolderName = Path.GetFileName( Path.GetDirectoryName( path ) );
    

    The inner call to GetDirectoryName will return the full path, while the outer call to GetFileName() will return the last path component – which will be the folder name.

    This approach works whether or not the path actually exists. This approach, does however, rely on the path initially ending in a filename. If it’s unknown whether the path ends in a filename or folder name – then it requires that you check the actual path to see if a file/folder exists at the location first. In that case, Dan Dimitru’s answer may be more appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a URL: URL url=new URL("https://example.com/aa/bb/cc/file.html"); and a relative path: String relativePath="../file2.html"; //maybe
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
I have created a function that will return the string path from my DB.
What I'm looking for is a method that works like captureScreenshot(String path), but instead
The following code needs to produce an XML file. string path = @c:\load\myFile.xml; SqlConnection
I have a string var (path) that represents a path: printfn Please enter a
string fileName = test.zip; string path = c:\\temp\\; string fullPath = path + fileName;
ASP.NET: How do you debug dynamically loaded user controls using LoadControl(string path);
import java.lang.System; public class Splitter{ public static void main(String args[]){ String path_separator = Path
i have some problems with rowspan: var doc1 = new Document(); doc1.SetPageSize(PageSize.A4.Rotate()); string path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.