Uri.AbsolutePath messes up path with spaces
Asked Answered
W

5

17

In a WinApp I am simply trying to get the absolute path from a Uri object:

Uri myUri = new Uri(myPath); //myPath is a string
//somewhere else in the code
string path = myUri.AbsolutePath;

This works fine if no spaces in my original path. If spaces are in there the string gets mangled; for example 'Documents and settings' becomes 'Documents%20and%20Setting' etc.

Any help would be appreciated!

EDIT: LocalPath instead of AbsolutePath did the trick!

Wodge answered 12/1, 2009 at 18:42 Comment(1)
note to all: this is a rare display of encoding ignoranceWodge
O
11

It's encoding it as it should, you could probably UrlDecode it to get it back with spaces, but it's not "mangled" it's just correctly encoded.

I'm not sure what you're writing, but to convert it back in asp.net it's Server.UrlDecode(path). You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.

Owe answered 12/1, 2009 at 18:50 Comment(2)
I'm not sure what you're writing, but in asp.net it's Server.UrlDecode(path);Owe
You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.Owe
H
13

This is the way it's supposed to be. That's called URL encoding. It applies because spaces are not allowed in URLs.

If you want the path back with spaces included, you must call something like:

string path = Server.URLDecode(myUri.AbsolutePath);

You shouldn't be required to import anything to use this in a web application. If you get an error, try importing System.Web.HttpServerUtility. Or, you can call it like so:

string path = HttpContext.Current.Server.URLDecode(myUri.AbsolutePath);
Hap answered 12/1, 2009 at 18:52 Comment(3)
sounds good - which reference should I add to do that in a WinApp?Wodge
sweet - so I assume there's nothing similar specific to WinApps?Wodge
Thanks for the answer - best solution in this case is to use LocalPath instead of AbsolutePath since it's a winapp and I prefer not use Server or HttpContext.Wodge
O
11

It's encoding it as it should, you could probably UrlDecode it to get it back with spaces, but it's not "mangled" it's just correctly encoded.

I'm not sure what you're writing, but to convert it back in asp.net it's Server.UrlDecode(path). You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.

Owe answered 12/1, 2009 at 18:50 Comment(2)
I'm not sure what you're writing, but in asp.net it's Server.UrlDecode(path);Owe
You also might be able to use LocalPath, rather than AbsolutePath, if it's a Windows app.Owe
G
7

Just use uri.LocalPath instead

Gerson answered 24/7, 2013 at 16:17 Comment(0)
P
3

Uri also has a couple of static methods - EscapeDataString and EscapeUriString.

Uri.EscapeDataString(uri.AbsolutePath) also works

Pettish answered 8/11, 2010 at 6:57 Comment(1)
You probably mean Uri.UnescapeDataStringLabelle
H
0

Use HttpUtility:

 HttpUtility.UrlDecode(uri.AbsolutePath)
Hitchhike answered 15/10, 2015 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.