Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit 44e2912

Browse files
committed
安装后C盘的权限处理
1 parent 2001bf1 commit 44e2912

File tree

17 files changed

+273
-21
lines changed

17 files changed

+273
-21
lines changed

Common/AdministratorUtil.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Security.Principal;
7+
using System.Management;
8+
using System.Diagnostics;
9+
using System.Reflection;
10+
using System.Windows;
11+
12+
namespace Common
13+
{
14+
public class AdministratorUtil
15+
{
16+
public static void RunAsAdmin(string argment="")
17+
{
18+
//Put this code in the main entry point for the application
19+
// Check if user is NOT admin
20+
if (!IsRunningAsAdministrator())
21+
{
22+
// Setting up start info of the new process of the same application
23+
ProcessStartInfo processStartInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().CodeBase);
24+
25+
// Using operating shell and setting the ProcessStartInfo.Verb to “runas” will let it run as admin
26+
processStartInfo.UseShellExecute = true;
27+
processStartInfo.Verb = "runas";
28+
processStartInfo.Arguments = argment;
29+
// Start the application as new process
30+
Process.Start(processStartInfo);
31+
32+
}
33+
34+
}
35+
36+
37+
/// <summary>
38+
/// Function that check's if current user is in Aministrator role
39+
/// </summary>
40+
/// <returns></returns>
41+
private static bool IsRunningAsAdministrator()
42+
{
43+
// Get current Windows user
44+
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
45+
46+
// Get current Windows user principal
47+
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
48+
49+
// Return TRUE if user is in role "Administrator"
50+
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
51+
}
52+
53+
// kill process by name
54+
public static void KillProcess(string name)
55+
{
56+
foreach (Process clsProcess in Process.GetProcesses())
57+
{
58+
if (clsProcess.ProcessName.Contains(name))
59+
{
60+
clsProcess.Kill();
61+
}
62+
}
63+
}
64+
}
65+
}

Common/ComUtil.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
35
using System.Linq;
46
using System.Text;
57
using System.Threading.Tasks;
8+
using System.Windows;
69

710
namespace Common
811
{
@@ -22,6 +25,9 @@ public static string GetParentDirectory(string path)
2225
{
2326
return System.IO.Path.GetDirectoryName(path);
2427
}
25-
28+
public static string GetCurrentExecDirectory()
29+
{
30+
return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
31+
}
2632
}
2733
}

Common/Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Reference Include="System.Xml" />
6060
</ItemGroup>
6161
<ItemGroup>
62+
<Compile Include="AdministratorUtil.cs" />
6263
<Compile Include="JsonHelper.cs" />
6364
<Compile Include="ComUtil.cs" />
6465
<Compile Include="DownloadFileHelper.cs" />

Common/version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"StickyNotesVersion": {
33
"MajorVersionNumber": 5,
44
"MinorVersionNumber": 0,
5-
"RevisionVersionNumber": 1
5+
"RevisionNumebr": 1
66
},
77
"UpdateAppVersion": {
88
"MajorVersionNumber": 1,
99
"MinorVersionNumber": 0,
10-
"RevisionVersionNumber": 1
10+
"RevisionNumebr": 1
1111
}
1212
}

DB/Config.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System;
1+
using Common;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using System.Windows.Forms;
68

79
namespace DB
810
{
@@ -14,7 +16,8 @@ private static string GetCurrentProjectPath
1416

1517
get
1618
{
17-
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location).Replace(@"\bin\Debug", "");//获取具体路径
19+
var str= ComUtil.GetCurrentExecDirectory();
20+
return str;
1821
}
1922
}
2023
public static string ConnectionString = @"DataSource=" + GetCurrentProjectPath + @"\data.sqlite";

DB/DB.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.116.0\lib\net46\System.Data.SQLite.Linq.dll</HintPath>
6161
</Reference>
6262
<Reference Include="System.Web" />
63+
<Reference Include="System.Windows.Forms" />
6364
<Reference Include="System.Xml.Linq" />
6465
<Reference Include="System.Data.DataSetExtensions" />
6566
<Reference Include="Microsoft.CSharp" />
@@ -80,6 +81,12 @@
8081
<None Include="App.config" />
8182
<None Include="packages.config" />
8283
</ItemGroup>
84+
<ItemGroup>
85+
<ProjectReference Include="..\Common\Common.csproj">
86+
<Project>{EB437726-8891-46DE-9D88-271242DB82BC}</Project>
87+
<Name>Common</Name>
88+
</ProjectReference>
89+
</ItemGroup>
8390
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8491
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
8592
<PropertyGroup>

StickyNotes/App.xaml.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ public partial class App : Application
3737
/// <param name="e"></param>
3838
protected override async void OnStartup(StartupEventArgs e)
3939
{
40+
4041
#region 是否只能运行一个APP
4142

4243
bool ret;
4344
mutex = new System.Threading.Mutex(true, "StickyNotesAPP", out ret);
4445

4546
if (!ret)
4647
{
47-
MessageBox.Show("程序已经运行了");
48-
Environment.Exit(0);
48+
AdministratorUtil.KillProcess("StickyNotes");
4949
}
5050
#endregion
51-
51+
#region 检测管理员权限
52+
AdministratorUtil.RunAsAdmin();
53+
#endregion
5254

5355
Logger.Log().Info("程序启动");
5456
/// 将全局异常保存到文件目录下
@@ -114,8 +116,8 @@ protected override async void OnStartup(StartupEventArgs e)
114116
private void CheckUpdate()
115117
{
116118
ProgramData p = ProgramData.Instance;
117-
//if (p.IsAutoCheckUpdate == false)
118-
// return;
119+
if (p.IsAutoCheckUpdate == false)
120+
return;
119121
try
120122
{
121123
var updateHelper=new UpdateHelper();

StickyNotes/StickyNotes.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</ManifestKeyFile>
6969
</PropertyGroup>
7070
<PropertyGroup>
71-
<GenerateManifests>true</GenerateManifests>
71+
<GenerateManifests>false</GenerateManifests>
7272
</PropertyGroup>
7373
<PropertyGroup>
7474
<SignManifests>false</SignManifests>
@@ -80,6 +80,12 @@
8080
<AssemblyOriginatorKeyFile>
8181
</AssemblyOriginatorKeyFile>
8282
</PropertyGroup>
83+
<PropertyGroup>
84+
<ApplicationManifest>app.manifest</ApplicationManifest>
85+
</PropertyGroup>
86+
<PropertyGroup>
87+
<TargetZone>LocalIntranet</TargetZone>
88+
</PropertyGroup>
8389
<ItemGroup>
8490
<Reference Include="System" />
8591
<Reference Include="System.Data" />
@@ -248,6 +254,7 @@
248254
<Generator>PublicResXFileCodeGenerator</Generator>
249255
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
250256
</EmbeddedResource>
257+
<None Include="app.manifest" />
251258
<None Include="Properties\Settings.settings">
252259
<Generator>SettingsSingleFileGenerator</Generator>
253260
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

StickyNotes/Utils/UpdateHelper.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class UpdateHelper
3838
/// <param name="revisionNumebr"></param>
3939
public void UpdateUpdateTool()
4040
{
41-
Common.Version version = JsonHelper.ReadVersionFromFile(Environment.CurrentDirectory,StickyNotes.Properties.Resources.VersionFileName);
41+
Common.Version version = JsonHelper.ReadVersionFromFile(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), StickyNotes.Properties.Resources.VersionFileName);
4242
var res = HttpHelper.HttpGet("api/Software/GetLastedVersion",
4343
new string[] { "softwarename", "majorVersionNumber", "minorVersionNumber", "revisionNumebr" },
4444
new object[] { "updateapp", version.UpdateAppVersion.MajorVersionNumber, version.UpdateAppVersion.MinorVersionNumber, version.UpdateAppVersion.RevisionNumebr });
@@ -71,15 +71,16 @@ public void UpdateUpdateTool()
7171

7272
private void DownloadFileHelper_ProcessCompleted()
7373
{
74-
Common.FileHelper.Decompress(UpdatePatchFilePath, Environment.CurrentDirectory+"update");
74+
var curDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
75+
Common.FileHelper.Decompress(UpdatePatchFilePath, Path.Combine(curDir, "update"));
7576
if (File.Exists(UpdatePatchFilePath))
7677
File.Delete(UpdatePatchFilePath);
7778
UpdateToolUpdated = true;
78-
var version=JsonHelper.ReadVersionFromFile(Environment.CurrentDirectory,StickyNotes.Properties.Resources.VersionFileName);
79+
var version=JsonHelper.ReadVersionFromFile(curDir, StickyNotes.Properties.Resources.VersionFileName);
7980
version.UpdateAppVersion.MajorVersionNumber = LatestUpdateToolVersion.major_version_number;
8081
version.UpdateAppVersion.MinorVersionNumber = LatestUpdateToolVersion.minor_version_number;
8182
version.UpdateAppVersion.RevisionNumebr = LatestUpdateToolVersion.revision_number;
82-
JsonHelper.WriteVersionToFile(version, Environment.CurrentDirectory,StickyNotes.Properties.Resources.VersionFileName);
83+
JsonHelper.WriteVersionToFile(version, curDir, StickyNotes.Properties.Resources.VersionFileName);
8384
UpdateToolCompleted?.Invoke();
8485

8586
}
@@ -94,7 +95,7 @@ private void DownloadFileHelper_ProgressChanged(string totalNum, string num, in
9495
/// <returns></returns>
9596
public bool CheckSelfNeedUpdate()
9697
{
97-
version = JsonHelper.ReadVersionFromFile(Environment.CurrentDirectory,StickyNotes.Properties.Resources.VersionFileName);
98+
version = JsonHelper.ReadVersionFromFile(ComUtil.GetCurrentExecDirectory(),StickyNotes.Properties.Resources.VersionFileName);
9899
HttpHelper.BaseUrl = ServerUrl;
99100
var res=HttpHelper.HttpGet("api/Software/GetLastedVersion", new string[] { "softwarename" }, new object[] { "stickynotes" });
100101
if (res!=null&&res.success)
@@ -118,7 +119,7 @@ public void OpenUpdateTool()
118119
{
119120
if (UpdateToolUpdated == false)
120121
return;
121-
var path = Path.Combine(Environment.CurrentDirectory, "update\\UpdateApp.exe");
122+
var path = Path.Combine(ComUtil.GetCurrentExecDirectory(), "update\\UpdateApp.exe");
122123
string args = version.StickyNotesVersion.MajorVersionNumber.ToString() + " " + version.StickyNotesVersion.MinorVersionNumber.ToString() + " " + version.StickyNotesVersion.RevisionNumebr.ToString();
123124
Process.Start(path,args);
124125
}

StickyNotes/View/AboutWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public AboutWindow()
2121
ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
2222
ThemeManager.Current.SyncTheme();
2323
this.DataContext = this;
24-
var ver= JsonHelper.ReadVersionFromFile(Environment.CurrentDirectory,StickyNotes.Properties.Resources.VersionFileName);
24+
var ver= JsonHelper.ReadVersionFromFile(ComUtil.GetCurrentExecDirectory(),StickyNotes.Properties.Resources.VersionFileName);
2525
Version = $"{ver.StickyNotesVersion.MajorVersionNumber}.{ver.StickyNotesVersion.MinorVersionNumber}.{ver.StickyNotesVersion.RevisionNumebr}";
2626
}
2727

0 commit comments

Comments
 (0)