Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

return a file info path

I have the following method that returns a string..

This method needs to return a file info object instead of a string.

I need the code refactored so it always returns a path instead of this scenario where it returns an empty string...I would not want it to return an empty fileinfo object either...as I validate this before the method is called.

I still need to get the path returned based on the logic below..
parent/child exists
child exists.
else
root


public string GetFolderPath(string rootPath, string parent, string child)
        {

            if (Directory.Exists(rootPath))
            {
                if (Directory.Exists(Path.Combine(rootPath, parent)))
                {
                    if (Directory.Exists(Path.Combine(rootPath, parent, child)))
                    {
                        return Path.Combine(rootPath, parent, child);
                    }
                    else
                    {
                        { return Path.Combine(rootPath, parent); }
                    }
                }
                else
                {
                    { return rootPath; }
                }
            }

            return "";
        }

Open in new window

SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robb Hill

ASKER

not sure..I ever want to hard code just a phony value for rootpath...perhaps I could refactor all together...

Here is how Im calling it.


    newdocumentpath = GetFolderPath(doc.SitePath, txtParent.Text, txtChild.Text);

I want the return value from the method to be a returnpath.  

Perhaps I could pass in GetFolderPath as a FileInfo object...then examine FileInfo object to determine which is the path.


OR if rootpath was the final return value ....this would be correct.

It should be return path root/parent/child
elseif
return path root/paretn
else return path root
>>This method needs to return a file info object instead of a string.
...
>>I need the code refactored so it always returns a path


Which do you want?  A path ( as a string ) or a FileInfo object - they are two very different things.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well perhaps all this code is incorrect..hehe.


This is just supposed to be a directory search to determine which is the directory I will use ...the decision is not based on any files...but only the existance of the directory structure.

When I step into the method I have a root directory which is validated as this is a path taken from my database.
The only validation I could think here would be to validate it really exists...just in case the database was incorrect.

But essentially I am given a parent folder and child folder parameter from the UI which is supposed to match up with a directory structure on the network.  

I have to iterate through the potential paths it could create and return them in the order I suggest.

So end the end the only return value is a directory path.

That path might be what is stored in rootpath...that could be something like /server/a/b/c/d
that path might be the root path + the parent + child so .../server/a/b/c/d/parent/child
or
just parent
/server/a/b/c/d/parent

If I am going to return a null..that seems more like im tricking the code..seems like the method should not excute if rootpath is invalid to begin with.

Then make the method return a directory ...in which it has 3 potential returns.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would like to work in fileinfo..not strings.

string root path would be the default path.

If nothing is found it should be an exception..not a return.


 public FileInfo GetFolderPath(string rootPath, string parent, string child)
        {

            if (Directory.Exists(rootPath))
            {
                if (Directory.Exists(Path.Combine(rootPath, parent)))
                {
                    if (Directory.Exists(Path.Combine(rootPath, parent, child)))
                    {
                        return new FileInfo(Path.Combine(rootPath, parent, child));
                    }
                    else
                    {
                        { return new FileInfo(Path.Combine(rootPath, parent)); }
                    }
                }
                else
                {
                    { return new FileInfo(rootPath); }
                }
            }

            return new FileInfo(null);
        }

Open in new window

From a previous comment:
>>So end the end the only return value is a directory path.
and your last comment
>>I would like to work in fileinfo..not strings.

I'm getting very confused.  As I said earlier a FileInfo and a Path (string) are two totally different things.  In fact a FileInfo is info about a file - not a folder.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I explained that I am examining directory paths not files to make the determination.

Does the fileinfo object not handle files and directories?
ok sorry for confusion...lets try this again:)


I call this method and would like a destination directory returned.  I thought the return type of fileinfo would make most sense here rather than a string.

 targetFile = GetFolderPath(doc.SitePath, txtParent.Text, txtChild.Text);

The parameters here are a sitepath stored from a linq call to my DB.  This will always return a value of some network share and would be the starting point of the directory..aka root.
The parent and child values are passed in from a winform..and represent physical values on a parent and child folder of a network.

The only purpose of this method is to take this sitepath record..and determine will the destination directory be in the child folder...if it exists..or the parent folder..if it exists...if neither exists..it will be in the directory represented as doc.sitepath.

If for any reason that doc.sitepath did not truly exist on the network ..this should be an exception.

Fileinfo...string...FileInfo was a preference as It would work with my other methods better so  I dont have to keep converting from strings to other objects.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Either code will work in regards to the solution but since you all say that exception driven logic is bad how should I handle.

I have a procedure before I call this method..in which I make a linq call to my database..in which I get this  root path.  It is a full network share path.  There is the small chance that this path could somehow have gotten invalid in the database.

Should I just validate there when I get the path.  ...So that this method would not have an exception to deal with?

Actually while typing this out that makes more sense..because I would never step into this method if that parameter was not legit.

I am going to refactor so that I can assume that at the very least the root always passed in is valid.

I am refactorying now and based on latest comments will propose a revised solution.

Thank you all for all your input so far..Very good discussion.
ok so in a higher level of the program I have done the following:

ever one of the doc objects in the docInfo list could represent a path...This list is ultimately coming from a linq call to a database.
Now a whole bunch of logic happens after this but my assumption here is to just move on to the next iteration of doc in docinfo if the path from the database is incorrect.


  foreach (var doc in docInfo)
            {
                if (!Directory.Exists(doc.SitePath)
                {
                    Globals.LogEntry(MethodBase.GetCurrentMethod(), "The path " + doc.SitePath  + " Does not                exist for " + doc.DocId);
                    continue;
                }
              // all the logic
                targetFile = GetFolderPath(doc.SitePath, txtParent.Text, txtChild.Text);

Open in new window

So now the target path method should work to this example I believe:


public DirectoryInfo GetFolderPath(string rootPath, string parent, string child)
{
	// root path has already been verified; this is our default.
	DirectoryInfo result = new DirectoryInfo(rootPath);
	try
	{
		if (Directory.Exists(Path.Combine(result.FullName, parent, child)))
			result = new DirectoryInfo(Path.Combine(result.FullName, parent, child));
		else if (Directory.Exists(Path.Combine(result.FullName, parent)))
			result = new DirectoryInfo(Path.Combine(result.FullName, parent));
	}
	catch (Exception)
	{
              //Should we still do the exception.  The code should work...but do we still code for it to go wrong or do we code for it to always be right:)  For now as I continue coding I will leave this exception in until I hear back from you all.  
	}
	return result;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all for all the input.  Its very hard to assign points when you all were so interactive.  Thanks for helping me wrap this piece up and learn something from all of your ideas and thought!