Skip to content

Commit

Permalink
修改提权方式,现在不强行要求管理员权限才能启动,但是仍必须以管理员身份运行才能正常使用(因为netsh需要管理员权限)
Browse files Browse the repository at this point in the history
  • Loading branch information
SIXiaolong1117 committed Sep 19, 2023
1 parent 8523b94 commit 43d4bd2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
4 changes: 4 additions & 0 deletions NetworkSelector/About.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<TextBlock Text="⚖️License:" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<HyperlinkButton Content="MIT License" FontSize="14" NavigateUri="https://github.com/Direct5dom/NetworkSelector/blob/master/LICENSE" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
<TextBlock Text="❓帮助:" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<HyperlinkButton Content="Github Wiki" FontSize="14" NavigateUri="https://github.com/Direct5dom/NetworkSelector/wiki" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</Grid>
</Page>
8 changes: 8 additions & 0 deletions NetworkSelector/NetSetting.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,13 @@
IsLightDismissEnabled="True"
CloseButtonContent="明白!">
</TeachingTip>
<TeachingTip x:Name="NotAdminTips"
Title="设置失败!"
Subtitle="您需要以管理员身份运行网关切换器 (Gateway Selector),这是因为netsh需要在管理员权限下才可以修改网络配置信息。"
PreferredPlacement="Auto"
PlacementMargin="20"
IsLightDismissEnabled="True"
CloseButtonContent="明白!">
</TeachingTip>
</Grid>
</Page>
36 changes: 25 additions & 11 deletions NetworkSelector/NetSetting.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
Expand Down Expand Up @@ -66,17 +67,30 @@ public NetSetting()
// 应用保存的设置
public void applyConfig()
{
Process process = new Process();
process.StartInfo.FileName = "PowerShell.exe";
process.StartInfo.Arguments = localSettings.Values["netshCMD"] as string;
//是否使用操作系统shell启动
process.StartInfo.UseShellExecute = false;
//是否在新窗口中启动该进程的值 (不显示程序窗口)
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
process.Close();
NetworkIsChangeTips.IsOpen = true;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);

bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

if (isAdmin)
{
Process process = new Process();
process.StartInfo.FileName = "PowerShell.exe";
process.StartInfo.Arguments = localSettings.Values["netshCMD"] as string;
//是否使用操作系统shell启动
process.StartInfo.UseShellExecute = false;
//是否在新窗口中启动该进程的值 (不显示程序窗口)
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
process.Close();
NetworkIsChangeTips.IsOpen = true;
}
else
{
NotAdminTips.IsOpen = true;
}

}
private void refreshCMD(string netInterface, string IPAddr, string Mask, string Gateway, string DNS1, string DNS2)
{
Expand Down
2 changes: 2 additions & 0 deletions NetworkSelector/NetworkSelector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<AppxBundlePlatforms>x86|x64|arm64</AppxBundlePlatforms>
</PropertyGroup>
</Project>
3 changes: 1 addition & 2 deletions NetworkSelector/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<Properties>
<DisplayName>NetworkSelector</DisplayName>
<PublisherDisplayName>SI_Xiaolong</PublisherDisplayName>
<PublisherDisplayName>SI Xiaolong</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

Expand Down Expand Up @@ -44,6 +44,5 @@

<Capabilities>
<rescap:Capability Name="runFullTrust" />
<rescap:Capability Name="allowElevation" />
</Capabilities>
</Package>
8 changes: 0 additions & 8 deletions NetworkSelector/app.manifest
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="NetworkSelector.app"/>

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
Expand Down

0 comments on commit 43d4bd2

Please sign in to comment.