How to get full path from "\Storage card\test folder\..\..\Windows"

K

Kosmo

Hi, experts!

I need to check if all paths has the same root folder. I receive
strings like "\Storage card\test folder\folder1" and check if
directory exists and string starts with "\Storage card\test folder\".
Problem appears when I receive path with ".." characters. Just check
out:

"\Storage card\test folder\..\..\Windows" starts with "\Storage card
\test folder\" and exists!
And there is no way to get system translation of "\Storage card\test
folder\..\..\Windows" to "\Windows"!
I've tried to use Path.GetFullPath() and DirectoryInfo.FullPath -
returns exactly the input!
But if I create new file with name "\Storage card\test folder\..\..
\Windows\test.txt" - it appears correctly in \Windows folder. So, it
means, system translates such path in some way.

Question: Is there any way to simplify this task with .NET CF classes
\PInvoke calls, or I should write my own parser like above

string GetFullPath(string filepath)
{
List<string> listParts = new List<string>();

try
{
string[] parts =
filepath.Split(Path.DirectorySeparatorChar);

foreach (var part in parts)
{

switch (part)
{
case ".":

//do nothing

break;

case "..":

listParts.RemoveAt(listParts.Count - 1);

break;

default:

listParts.Add(part);

break;
}
}
}
catch (Exception ex)
{
throw new IOException("Path is invalid", ex);
}

return String.Join(Path.DirectorySeparatorChar.ToString(),
listParts.ToArray());
}

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top