怎么判断一个目录(文件夹)是否存在,不存在创建之
给出代码

解决方案 »

  1.   

    Determines whether a specified directory exists.UnitSysUtilsCategoryfile management routinesDelphi syntax:function DirectoryExists(const Directory: string): Boolean;C++ syntax:extern PACKAGE bool __fastcall DirectoryExists(const AnsiString Directory);DescriptionCall DirectoryExists to determine whether the directory specified by the Name parameter exists. If the directory exists, the function returns true. If the directory does not exist, the function returns false.If a full path name is entered, DirectoryExists searches for the directory along the designated path. Otherwise, the Name parameter is interpreted as a relative path name from the current directory.The FileCtrl unit (Windows only) also contains a DirectoryExists function. However, the FileCtrl version is deprecated, and the SysUtils version preferred, even if the code does not need to be cross-platform.
      

  2.   

    The following example creates a directory 慍:\temp?if it does not already exist.uses FileCtrl;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not DirectoryExists('c:\temp') then
        if not CreateDir('C:\temp') then
        raise Exception.Create('Cannot create c:\temp');
    end;
      

  3.   

    if not DirectoryExists('c:\temp') then
       CreateDir('c:\temp')
      

  4.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      s:string;
    begin
      s:='m:\xx';
      if not DirectoryExists(s) then
            mkdir(s);end;
      

  5.   

    判断文件
    var
      Text :TextFile;
    begin
      if FileExists(文件名全路径) = False then   //如果文件不存在
      begin
        AssignFile(Text, 你需要创建的文件全路径);
        Reset(Text);
        CloseFile(Text);
      end;
    end;//检查目录是否存在
      if DirectoryExists(目录全路径) = False then  //如果目录不存在
        ChDir(目录全路径);
      

  6.   

    DirectoryExists functionDetermines whether a specified directory exists.UnitSysUtilsor FileCtrlCategoryfile management routinesfunction DirectoryExists(const Directory: string): Boolean;DescriptionCall DirectoryExists to determine whether the directory specified by the Name parameter exists. If the directory exists, the function returns True. If the directory does not exist, the function returns False.If a full path name is entered, DirectoryExists searches for the directory along the designated path. Otherwise, the Name parameter is interpreted as a relative path name from the current directory.Note: DirectoryExists is also found in the FileCtrl unit.
      

  7.   

    创建目录错了,应该改成CreateDir(目录全路径)
      

  8.   

    if DirectoryExists(文件名全路径) = False then   //如果文件不存在
        CreateDir(目录全路径);