如何判断一个输入的路径是否有效。(delphi有没有自带的函数)(50分)

三石

Unregistered / Unconfirmed
GUEST, unregistred user!
比如输入的路径为 g/abc/fdc
如何能判断其有效性
 
来自:zwhc, 时间:2000-12-7 20:09:00, ID:412521
windows 帮助里提到:
文件名至多包含 255 个字符(包括空格)。不能包含以下字符: / / : * ? " < > |

for i:=1 to length(s) do
if s in ['/','/',':','*','?','<','>','|'] then
//非法


 
OpenDialog 、SaveDialog 之类的对话框就能判断路径是否有效。。。
 
那如何建立符合目录命名但不存在的目录呢。
我想让目录目录检查和建立这些一起实现。
(我想实现对输入的一串字符串进行有效性检查,
if check(s) then
if exist(s) then
SetCurrentDir(s); //将该目录设为当前目录
else
begin
Create(s);   //创建该目录
SetCurrentDir(s)
end
else
showmessage('Error')
 
function DirectoryExists(Name: string): Boolean;

///////////////////////////////////////////////////
uses ...,FileCtrl;

procedure TForm1.Button1Click(Sender: TObject);
begin
if DirectoryExists('c:/') then showmessage('Ok');
if DirectoryExists('c:/windows') then showmessage('Ok');
if DirectoryExists('c:/windows/') then showmessage('Ok');
if DirectoryExists('g/abc/fdc') then showmessage('Ok')
else showmessage('No No No');
end;

 
s:string;
begin
s:=trim(s);
if length(s)=0 then exit;
if (DirectoryExists(s) then
showmessage('已存在')
else
if ForceDirectories(s) then showmessage('建立成功')
else showmessage('无法建立');
 
jsxjd,说得对not DirectoryExists(s) 并不表示路径无效,如果
ForceDirectories(s) 不成功才是无效路径
 
begin

{$I-}
{ Change to directory specified in Edit1 }
ChDir(Edit1.Text);
if IOResult <> 0 then
MessageDlg('Cannot find directory', mtWarning, [mbOk], 0);

end;
 
散分了。
谢谢 jsxjd 了!!!!!!
 
顶部