2010-04-15 17 views
4

我需要從程序根目錄打開一個html文件,並讓它跳轉到指定的錨點。我可以打開該文件用一個簡單的用文件中的HTML啓動默認瀏覽器,然後跳轉到特定的錨點

System.Diagnostics.Process.Start(「site.html」)

完全正常,但只要我嘗試錨添加到最終它不再能找到該文件。

我能夠把錨在那裏,仍然有它與

串錨

推出

錨=「文件:///」 + Environment.CurrentDirectory.ToString()更換(」 \「,」/「)+ 」/site.html#Anchor「; System.Diagnostics.Process.Start(Anchor);

但是,只要瀏覽器啓動,它就會拋錨。有什麼建議麼?

回答

2

您可能需要將整個URL放在引號中以保留任何特殊字符(如#)或空格。

嘗試:

string Anchor = String.Format("\"file:///{0}/site.html#Anchor\"", Environment.CurrentDirectory.ToString().Replace("\\", "/")); 
System.Diagnostics.Process.Start(Anchor); 
+0

不爲我工作..不知道爲什麼? Vista/IE7 – 2010-05-13 20:15:39

+0

任何解決方案RunCMD? – Kiquenet 2011-10-28 11:17:10

5
using Microsoft.Win32; // for registry call. 

private string GetDefaultBrowserPath() 
{ 
    string key = @"HTTP\shell\open\command"; 
    using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false)) 
    { 
     return ((string)registrykey.GetValue(null, null)).Split('"')[1]; 
    } 
} 

private void GoToAnchor(string url) 
{ 
    System.Diagnostics.Process p = new System.Diagnostics.Process(); 
    p.StartInfo.FileName = GetDefaultBrowserPath(); 
    p.StartInfo.Arguments = url; 
    p.Start(); 
} 

// use: 
GoToAnchor("file:///" + Environment.CurrentDirectory.ToString().Replace("\", "/") + "/site.html#Anchor"); 
相關問題