응용프로그램에서 웹 페이지를 여는 방법은 무엇입니까?
WPF 응용 프로그램에서 기본 브라우저를 열고 특정 웹 페이지로 이동합니다.그걸 어떻게 하는 거죠?
.NET 데스크톱 버전의 경우:
System.Diagnostics.Process.Start("http://www.webpage.com");
.NET Core의 경우 의 기본값이 다음과 같이 변경되었습니다.true
로.false
그래서 당신은 그것을 명시적으로 설정해야 합니다.true
이 기능이 작동하려면:
System.Diagnostics.Process.Start(new ProcessStartInfo
{
FileName = "http://www.webpage.com",
UseShellExecute = true
});
문제를 더욱 복잡하게 만들기 위해 이 속성을 다음으로 설정할 수 없습니다.true
UWP 앱의 경우(따라서 이러한 솔루션은 UWP에 사용할 수 없습니다).
승인된 응답은 더 이상 .NET Core 3에서 작동하지 않습니다.작동하려면 다음 방법을 사용합니다.
var psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start (psi);
이 줄을 사용하여 기본 브라우저를 시작했습니다.
System.Diagnostics.Process.Start("http://www.google.com");
좋은 답변이 제공되는 동안(사용)Process.Start
), 전달된 문자열이 실제로 URI인지 확인하는 함수로 캡슐화하는 것이 기계에서 실수로 임의의 프로세스를 시작하지 않도록 하는 것이 더 안전합니다.
public static bool IsValidUri(string uri)
{
if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
return false;
Uri tmp;
if (!Uri.TryCreate(uri, UriKind.Absolute, out tmp))
return false;
return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}
public static bool OpenUri(string uri)
{
if (!IsValidUri(uri))
return false;
System.Diagnostics.Process.Start(uri);
return true;
}
여기 제 완전한 코드를 여는 방법이 있습니다.
두 가지 옵션이 있습니다.
기본 브라우저를 사용하여 열기(브라우저 창 안에서 열기)
기본 명령 옵션을 통해 열기("RUN.EXE" 명령을 사용하는 것과 같은 동작)
'dll'을 통해 열기(폴더 창 URL 안에 url을 작성한 것과 같습니다.)
[선택적 제안] 4. iexplore 프로세스 위치를 사용하여 필요한 URL을 엽니다.
코드:
internal static bool TryOpenUrl(string p_url)
{
// try use default browser [registry: HKEY_CURRENT_USER\Software\Classes\http\shell\open\command]
try
{
string keyValue = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\http\shell\open\command", "", null) as string;
if (string.IsNullOrEmpty(keyValue) == false)
{
string browserPath = keyValue.Replace("%1", p_url);
System.Diagnostics.Process.Start(browserPath);
return true;
}
}
catch { }
// try open browser as default command
try
{
System.Diagnostics.Process.Start(p_url); //browserPath, argUrl);
return true;
}
catch { }
// try open through 'explorer.exe'
try
{
string browserPath = GetWindowsPath("explorer.exe");
string argUrl = "\"" + p_url + "\"";
System.Diagnostics.Process.Start(browserPath, argUrl);
return true;
}
catch { }
// return false, all failed
return false;
}
및 도우미 기능:
internal static string GetWindowsPath(string p_fileName)
{
string path = null;
string sysdir;
for (int i = 0; i < 3; i++)
{
try
{
if (i == 0)
{
path = Environment.GetEnvironmentVariable("SystemRoot");
}
else if (i == 1)
{
path = Environment.GetEnvironmentVariable("windir");
}
else if (i == 2)
{
sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
path = System.IO.Directory.GetParent(sysdir).FullName;
}
if (path != null)
{
path = System.IO.Path.Combine(path, p_fileName);
if (System.IO.File.Exists(path) == true)
{
return path;
}
}
}
catch { }
}
// not found
return null;
}
내가 도와줬기를 바랍니다.
높은 응용프로그램에서 웹 페이지를 시작할 수 없습니다.explor.exe 및 브라우저가 승격되지 않은 상태로 실행되고 있기 때문에 0x800004005 예외가 발생합니다.
승격되지 않은 웹 브라우저에서 상승된 응용프로그램에서 웹 페이지를 시작하려면 Mike Feng이 만든 코드를 사용합니다.ApplicationName을(를) 지원하기 위해 URL을 전달하려고 했지만 작동하지 않았습니다.또한 CreateProcess를 사용할 때도 마찬가지입니다.tokenW with lpApplicationName = "slap.exe"(또는 iexplore.exe) 및 lpCommandLine= url.
다음 해결 방법은 작동합니다. 프로세스라는 한 가지 태스크가 있는 작은 EXE 프로젝트를 생성합니다.시작(url), CreateProcess 사용TokenW를 사용하여 이 .EXE를 실행합니다.Windows 8 RC에서는 이것이 잘 작동하고 Google Chrome에서 웹 페이지를 엽니다.
예전의 학교 방식 ;)
public static void openit(string x) {
System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
}
사용:openit("www.google.com");
저는 오늘 비슷한 문제가 있어서 이에 대한 해결책을 가지고 있습니다.
관리자 권한으로 실행되는 앱에서 http://google.com 을 열고 싶다고 가정합니다.
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo);
string target= "http://www.google.com";
try
{
System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
언급URL : https://stackoverflow.com/questions/502199/how-to-open-a-web-page-from-my-application
'itsource' 카테고리의 다른 글
현재 어셈블리의 경로를 가져오는 중 (0) | 2023.05.27 |
---|---|
Postgre에서 데이터베이스 복사본 작성SQL (0) | 2023.05.27 |
노드 - NODE_MODULE_VERSION 51을 사용하여 다른 Node.js 버전에 대해 컴파일되었습니다. (0) | 2023.05.27 |
jQuery set 확인란 선택 (0) | 2023.05.27 |
Node.js에 대한 Haskell의 응답은 무엇입니까? (0) | 2023.05.27 |