我正在使用C#.net WPF开发的应用程序,我想知道如何使应用程序每次启动,
使用管理员帐户完成安装。
应用程序的检测代码是否应在“ App.XAML.cs”类中?
启动应用程序的方法的代码为:
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException +=
new
try
{
// Créé le répertoire des traces s'il n'existe pas déjà
string cwd = Directory.GetCurrentDirectory();
string logPath = Path.Combine(cwd, "log");
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
// Log4Net configuration
FileInfo log4NetConfig = new FileInfo(Path.Combine(cwd,
"Log4net.config"));
log4net.Config.XmlConfigurator.Configure(log4NetConfig);
// Récupère la version de l'application
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetExecutingAssembly();
Version version = assembly.GetName().Version;
UpdateService.CheckingForUpdates();
Log.Info("Démarrage de l'application " +
OtherHelper.GetAppSetting("NomApplication", "N.O.E") + "
version " + version);
ConfigService.InitializeConfigPathAndModificationDate();
TagService.InitializeReferential();
}
catch (Exception ex)
{
////////
}
base.OnStartup(e);
}
答案 0 :(得分:0)
根据所需的AppData文件夹,可以使用Environment.GetFolderPath()
检索所需的文件夹。
因此,要检查AppData \ Roaming \ N.O.E中的目录“ Affaires”,请创建目录(如果不存在),例如:
string appDataLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string affairesDirPath = appDataRoamingPath + "\\N.O.E.\\Affaires";
if(!Directory.Exists(affairesDirPath))
{
DirectoryInfo affairesDir = Directory.CreateDirectory(affairesDirPath);
//Do anything else you need to with the directory here.
}
如果只需要创建目录而无需执行任何其他操作,则可以简单地使用Directory.CreateDirectory(affairesDirPath);
;如果目录已经存在,它将不会创建该目录。
对于其他目录,您可以执行以下操作:
string affairesDirPath = "C:\\N.O.E.\\Affaires";
if(Directory.Exists(affairesDirPath))
{
//Move the directory
}