views:

40

answers:

3

Hello All-

I'm having trouble with a URL string in my Web Application. It's using a UNC path similar to \\houtestmachine\common\File1.pdf My problem is when it encounters files that have a # character. I tried doing a string newstring = originalstring.Replace("#", "%23"); but the # is still there in URL (target of a hyperlink) at runtime in the browser. How can I fix this?

+1  A: 

Have you tried HttpUtility.UrlEncode()?

mjmarsh
Yes I tried string NewString = System.Web.HttpUtility.UrlEncode(OriginalString); and at runtime that does something funky with my URL string. Instead of it being a UNC path it becomes http://servername/extrastuff..then it tries to include location of files. I need UNC path set just like it was but without #. Did I set it up wrong?
Josh
did I set it up incorrectly?
Josh
A: 

Use symbol @ befor string. For example

string st = @"your#path"

Constantine
+2  A: 

You are converting between file system paths and URLs. The Uri class should fit the bill:

using System;

class Program {
  static void Main(string[] args) {
    var url = new Uri(@"\\houtestmachine\common\F#ile1.pdf");
    Console.WriteLine(url.AbsoluteUri);
    var back = url.LocalPath;
    Console.WriteLine(back);
    Console.ReadLine();
  }
}

Output:

file://houtestmachine/common/F%23ile1.pdf
\\houtestmachine\common\F#ile1.pdf
Hans Passant
Hans- I think you are on to something here but my problem still persist. Let me explain further beacuse my example may not be perfect. My loop is also finding files that have a name structure like "5678_Mills 09-14 #2-3H_CBL.tif" And with your code it does seem to initially set it to wack wack houtestmachine/common/5678_Mills%2009-14%20%232-3H_CBL.tif then I have to set the hyperlink.NavigateUrl to this uri.absoluteUri and when I hover over this hyperlink in the browser I notice it says wack wack houtestmachine/common/5678_Mills%2009-14%20#2-3H_CBL.tif
Josh
So does this have something to do with the hyperlink or is there an issues with there being a space infront of the #? I have no clue but there are many files with that structure that I need to find a way to get this to work. Thanks for any help
Josh
Why is the file:// prefix missing? It should be important.
Hans Passant
It's there but when I leave a comment this site removes it. I noticed when I initially set a question up I have the option to use the include code button and then it will show up but when i leave comments i don't see that button anymore
Josh
I got no trouble typing file://. Are you using backslashes? Don't.
Hans Passant
Hi Hans- After looping to find the files and then using your code, yes initially it is set to "file://houtestserver/common/Geology/Log%20Library/GRAPHICS/03-Arkansas/141-test/03141101740000_Mills%2009-14%20%232-3H_CBL_1500PSI.tif" Then that string gets set to a Hyperlink.Navigate and then Hyperlink.Rendercontrol is set on HTMLTextWriter. So when I launch my website and I hover over that hyperlink, I see in status bar "file://houtestserver/common/Geology/Log%20Library/GRAPHICS/03-Arkansas/141-test/03141101740000_Mills%2009-14%20%#2-3H_CBL_1500PSI.tif" The # is back but not sure if an issue
Josh
When I click the hyperlink a window opens and says Internet Explorer cannot display the webpage and the URL is showing "\\houtestserver\common\GEOLOGY\Log Library\GRAPHICS\03-Arkansas\141-test\03141101740000_Mills 09-14" Notice it flipped the slashes and didn't get past the space and # part of the path
Josh