我正在使用TaskScheduler在部署期間設定一個計劃任務。同時,我正在使用一個設定檔案來存盤用戶配置。現在當任務調度程式運行時,它不能訪問設定檔案,因為用戶不匹配。
在安裝程序中,設定專案作為系統運行,以獲得應用程式目錄中的寫入權限。我正在運行一個自定義動作來寫入設定和預定任務,但用戶并不匹配。
我如何才能確保用戶匹配。我是否必須創建一個單獨的應用程式來進行配置,以便設定可以在應用程式范圍內?這對我來說毫無意義,因為這些設定在每個部署中都是不同的,而且是由用戶而不是我設定的。
編輯:用戶設定檔案的位置
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ApplicationName") 。
if (! Directory.Exists(path)) Directory.CreateDirectory(path)。
Directory.SetCurrentDirectory(path)。
創建計劃任務的腳本片段
using Microsoft.Win32.TaskScheduler;
...
if (Interval == null) Interval = TimeSpan.FromHours(1)。
if (Duration == null) Duration = TimeSpan.FromDays(1)。
using(TaskService ts = new TaskService()
{
var td = ts.NewTask();
//試圖獲得正確的用戶,獲得系統用戶,就像忽略了一樣。
td.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name。
td.Principal.LogonType = TaskLogonType.InteractiveToken;
td.Settings.ExecutionTimeLimit = TimeSpan.FromMinutes(5)。
var trigger = new DailyTrigger( (short)DaysInterval)
{
StartBoundary = DateTime.Today,
Repetition = new RepetitionPattern(Interval, Duration)
};
td.Triggers.Add(trigger)。
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
td.Actions.Add("" exePath "", "", Path.GetDirectoryName(exePath))。
ts.RootFolder.RegisterTaskDefinition("myTask"/span>, td)。
這個自定義動作,可以說是太大了,而且被分割在不同的檔案上,就像專案一樣,無法在這里分享
編輯:設定檔案的欄位的例子Properties.Settings _s = Properties.Settings.Default;
_s.ClientIp = Tx_Ip.Text;
_s.ShopID = Tx_ShopId.Text;
_s.Save()。
編輯:我現在已經在設定期間和通過計劃任務運行時都記錄了用戶、UserPath和AppPath。
Setup
User: NT-AUTORIT?TSYSTEM
用戶路徑。C:UsersuserAppDataLocalappname
AppPath: C:程式檔案(x86)usinessnameappname
計劃中的任務
User: NT-AUTORIT?TSYSTEM
UserPath: C:Windowssystem32configsystemprofileAppDataLocalappname
AppPath: C:程式檔案(x86)usinessnameappname
所以實際上是同一個用戶,但UserPath是不同的。由于我在設定程序中撰寫了設定,它們被保存在錯誤的位置,后來在計劃任務中無法訪問。
uj5u.com熱心網友回復:
這個問題的解決方案是使用一個單獨的專案來執行自定義操作,并且只使用應用程式范圍來進行設定。這樣,配置可以在安裝期間創建。如果應用程式被部署到設定的默認檔案夾中,則只能以管理員權限更改設定。
概念代碼的證明:
主應用程式專案
using System;
namespaceMyApplication
{
class Program {
{
static void Main(string[] args)?
{
Console.WriteLine(Properties.MyApplication.Default.Setting)。
Console.ReadLine()。
}
}
}
自定義動作專案
using System;
using System.Configuration;
使用System.IO.Definition; 使用System.IO.Definition
using System.Reflection;
namespaceWriteAppSettingsTest
{
class Program {
{
static void Main(string[] args)?
{
string exePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MyApplication.exe") 。
Console.WriteLine(exePath)。
Console.ReadLine();
配置cfg = ConfigurationManager.OpenExeConfiguration(exePath);
ClientSettingsSection = (ClientSettingsSection)cfg.GetSectionGroup("applicationSettings").Sections["MyApplication.Properties.MyApplication"]。
section.Settings.Get("Setting").Value.ValueXml.InnerText = "已編輯的設定"。
section.SectionInformation.ForceSave = true;
cfg.Save()。
}
}
主應用程式需要一個設定檔案。在這個例子中,我把它叫做 "MyApplication.settings"。它需要一個名為 "Setting "的設定。在這個例子中,我把這個值設定為 "默認值"。
在設定專案中,在提交自定義動作下執行自定義動作專案。Installer類屬性必須設定為false才能像這樣作業。
這個解決方案還假設兩個專案的輸出都位于同一個檔案夾中
。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307249.html
標籤:
上一篇:在文本框中顯示多個復選框的選擇
