启动应用程序后在AppData中创建目录

时间:2019-04-29 12:28:29

标签: c# .net wpf xaml

我正在使用C#.net WPF开发的应用程序,我想知道如何使应用程序每次启动,

  1. 检测“%AppData%\ Roaming \ N.O.E”中是否有“ Affaires”目录,如果没有,则创建目录。
  2. 检测“ C:\ N.O.E”中是否有“ Affaires”目录,如果存在,请提示用户是否要将目录移至AppData。如果是,请移动目录。

使用管理员帐户完成安装。

应用程序的检测代码是否应在“ 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);
    }

1 个答案:

答案 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
}