Monday, February 21, 2011

Getting the physical path

I have to fetch all the files from a folder and i am using the function GetFiles() like

string[] dirImages = Directory.GetFiles(strPathYearImages + intYear , "*.png");

where strPathYearImages="Images\Holiday\2010\"

but when i write the whole path like

string[] dirImages = Directory.GetFiles(@"E:\IWP\Images\Holiday\"+ intYear , "*.png");

i get the required result.

How to solve this problem? I dont want to use the whole path. Help me out.

Regards, Jigar <3

From stackoverflow
  • The problem is the first snippet tries to get the images under current path. So you could tell the images path relative to your current path.

    Brigadier Jigar : Yes but how can i get the whole path? It is like the Images folder can be placed anywhere so i have to make the path dynamicaccording to the path where Images folder is placed.
    thelost : You cannot determine the full path to something you do not know where it resides. So you have to search fist.
  • Maybe it's because you are running it from VS inside. Your executable is in ProjectName\bin\Debug, therefore it looks for ProjectName\bin\Debug\Images, which obviously does not exists.

    Brigadier Jigar : No it is not the case. Thank You.
  • The documentation for GetFiles() says:

    The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory

    So you would want to make sure the current working directory is set correctly before trying to use the relative paths (eg to E:\IWP):

    GetCurrentDirectory

    SetCurrentDirectory

  • Use Application.StartupPath to get the location where the executable is running. From there you need to know where the images directory is relative to that directory. The only other option is the absolute path. How else would it know where to look?

    You can also try using System.IO.Path methods to help - especially for combining path strings, plus it also gives you the location of special folders like My Documents, AppData, and the desktop.

  • Hey all, i got the answer to my question. I just had to write

    string[] dirImages = HttpContext.Current.Server.MapPath(strPathImages + intYear , "*.png");

    Hope that it is helpful to all...

0 comments:

Post a Comment