开发者

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


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.


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜