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 1089489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:13:49+00:00 2026-05-16T23:13:49+00:00

We use absolut path or relatvie path to find a file in our C#

  • 0

We use absolut path or relatvie path to find a file in our C# application right now. Is there a way to find the file just by its name if the file is under the currect working directory or under one of the “paths”?

Use absolute pathe is not good and use relative path is not good enough because we may change project structure by rename or move project folders. If we our code can automatically search current working directory, its subfolders and search system path, that will be more flexible.

thanks,

  • 1 1 Answer
  • 0 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-16T23:13:50+00:00Added an answer on May 16, 2026 at 11:13 pm

    Try this:

    string target = "yourFilenameToMatch";
    string current = Directory.GetCurrentDirectory();
    
    // 1. check subtree from current directory
    matches=Directory.GetFiles(current, target, SearchOption.AllDirectories);
    if (matches.Length>0)
        return matches[0];
    
    // 2. check system path
    string systemPath = Environment.GetEnvironmentVariable("PATH");
    char[] split = new char[] {";"};
    foreach (string nextDir in systemPath.Split(split))
    {
        if (File.Exists(nextDir + '\\' + target)
        {
            return nextDir;
        }
    }
    
    return String.Empty;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're converting our Selenium tests to use Selenium-Grid. I'm trying to find a way
Is there any way to use Relative path when configuring subversion externals. For example
Is there a way to use a image url relative path in a javascript
Use case: I've just entered insert mode, and typed some text. Now I want
What is the best way to find a path relative to the folder where
What's the most concise (but safe) way to remove a drive name, network path,
Is there any way I can embed the images within my exe (as resource?)
I have an absolute path to file A. I have a relative path to
urlpatterns = patterns('', # Examples: url(r'^$', 'core.views.homepage', name='homepage'), url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), ) That's
There is a text file that I have created in my project root folder.

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.