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

C# 启动我的应用程序后在AppData中创建目录,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我正在开发一个用C#.net WPF开发的应用程序,我想知道如何启动我的应用程序 检测“%AppData%\Roaming\N.O.E”中是否有“Affaires”目录,如果没有,则创建该目录 检测“C:\N.O.E”中是否有“Affaires”目录,如果有,则提示用户是否要将该目录移动到AppData。如果是,请移动目录 安装是使用管理员帐户完成的 我的应用程序的检测代码是否应该在类“App.XAML.cs”中 启动应用程序的方法的代码为: protected override voi

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

  • 检测“%AppData%\Roaming\N.O.E”中是否有“Affaires”目录,如果没有,则创建该目录
  • 检测“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);
        }
    

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

    因此,要在AppData\Roaming\N.O.E中检查目录“Affaries”,请在不存在时创建它,例如:

    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
    }
    

    您的具体问题是什么?谢谢您的回复,我还想将目录“Affairs”和“C:\\N.O.E.\\Affairs”的子目录移动到%AppData%\Roaming\NOE\Affairs。我使用System.IO.directory.move(文件,businessFolder)但它不起作用。什么不起作用?你有错误吗?它在尝试移动它之前能找到目录吗?我使用了这段代码,它起作用了:DirectoryInfo dirInf=new DirectoryInfo(affairesFolder);foreach(dirs中的var subdir){string tempath=Path.Combine(affairesFolder,subdir.Name);如果(!Directory.Exists(temppath)){try{Directory.Move(subdir.FullName,temppath);}@阿里:太好了!如果我的答案对你有帮助,请务必将其标记为答案并进行投票,请:)