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



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

  1. 检测"%AppData%\Roaming\N.O.E"中是否存在"事务"目录,如果没有,则创建该目录。
  2. 检测"C:\N.O.E"中是否存在"事务"目录,如果有,则提示用户是否要将目录移动到 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);
    }

根据所需的 AppData 文件夹,您可以使用 Environment.GetFolderPath() 检索所需的文件夹。

因此,要检查 AppData\Roaming\N.O.E 中的目录"事务",请创建它(如果它不存在(,例如:

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
}

最新更新