Check if a folder exist in a directory and create them using C#

前端 未结 7 820
青春惊慌失措
青春惊慌失措 2020-12-01 02:44

How can I check if directory C:/ contains a folder named MP_Upload, and if it does not exist, create the folder automatically?

I am using V

相关标签:
7条回答
  • 2020-12-01 03:17
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace DirCombination 
    {
        public partial class DirCombination : Form
        {
            private const string _Path = @"D:/folder1/foler2/folfer3/folder4/file.txt";
            private string _finalPath = null;
            private string _error = null;
    
            public DirCombination()
            {
                InitializeComponent();
    
                if (!FSParse(_Path))
                    Console.WriteLine(_error);
                else
                    Console.WriteLine(_finalPath);
            }
    
            private bool FSParse(string path)
            {
                try
                {
                    string[] Splited = path.Replace(@"//", @"/").Replace(@"\\", @"/").Replace(@"\", "/").Split(':');
                    string NewPath = Splited[0] + ":";
                    if (Directory.Exists(NewPath))
                    {                    
                        string[] Paths = Splited[1].Substring(1).Split('/');
    
                        for (int i = 0; i < Paths.Length - 1; i++)
                        {
                            NewPath += "/";
                            if (!string.IsNullOrEmpty(Paths[i]))
                            {
                                NewPath += Paths[i];
                                if (!Directory.Exists(NewPath))
                                    Directory.CreateDirectory(NewPath);
                            }
                        }
    
                        if (!string.IsNullOrEmpty(Paths[Paths.Length - 1]))
                        {
                            NewPath += "/" + Paths[Paths.Length - 1];
                            if (!File.Exists(NewPath))
                                File.Create(NewPath);
                        }
                        _finalPath = NewPath;
                        return true;
                    }
                    else
                    {
                        _error = "Drive is not exists!";
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    _error = ex.Message;
                    return false;
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题