Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Removing %20 from URI Relative Path

I am generating a relative path from 1 directory to another. If the OutputDirectoryName property is a directory containing spaces, the spaces are encoded using %20, rather than a space. I am creating a relative path to a windows folder, so I must have my relatiave path using spaces. Is there a clean way to specify how the URI is encoded? I know I could do a stirng replace on the relativePath.ToString(), but am wondering if there's a better implementation. Thanks.

public string GetOutputDirectoryAsRelativePath(string baseDirectory)
{
    Uri baseUri = new Uri(baseDirectory);
    Uri destinationUri = new Uri(OutputDirectoryName);
    Uri relativePath = baseUri.MakeRelativeUri(destinationUri);
    return relativePath.ToString();
}

Upvotes: 26

Views: 26041

Answers (4)

darth happyface
darth happyface

Reputation: 2757

You can use

Uri.UnescapeDataString

http://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring.aspx

Upvotes: 42

Nisha
Nisha

Reputation: 1843

string sRelativeFilePath = Uri.UnescapeDataString(new Uri(sAbsolutePath + "\\", false).MakeRelative(new Uri(filename)));

Upvotes: 2

Jason Kealey
Jason Kealey

Reputation: 7986

Try looking at Server.UrlDecode: http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

The space character is not the only one that is encoded.

Upvotes: 1

Related Questions