■■怪问题:关于用函数复制文件夹的pchar问题■■ ( 积分: 60 )

  • 主题发起人 主题发起人 toniry
  • 开始时间 开始时间
T

toniry

Unregistered / Unconfirmed
GUEST, unregistred user!
我在做文件夹内容复制的时候,发现当源文件夹的长度字符串(如c:/aaa/bbb/)超过一定长度时(比如45个字符),运行复制就提示“无法复制文件:无法读取源文件或磁盘”的错误。估计是在字符串转换成pchar类型时发生问题。
用如下的写法对于有的复制文件夹函数(我在网上找到的函数)没问题,有的程序段有出现上述提示:
PChar('C:/Documents and Settings/new/Local Settings/Application Data/Identities/{7238B769-F427-4DDE-BFCE-2B9BFCA21BE7}/Microsoft/Outlook Express');
对于上面没问题的函数,当把中间的变量换成字符串相加是,还是会出现错误提示。
PChar('C:/Documents and Settings/'+UserName+'/Local Settings/Application Data/Identities/'+UserID+'/Microsoft/Outlook Express');

整个函数如下,朋友们帮我看看。估计是在字符串转换成pchar类型时发生问题,但不知怎么解决:

procedure TForm1.BackupFile;
var
sPath,tPath:string;
fsTemp:SHFILEOPSTRUCT;
i:integer;
begin
sPath := 'c:/bu';
//tPath := 'C:/Documents and Settings/'+UserName+'/Local Settings/Application Data/Identities/'+UserID+'/Microsoft/Outlook Express' ;
//tPath := 'C:/Documents and Settings/'+UserName+'/Local Settings/Application Data/Adobe/Color' ;
if sPath <> '' then
begin
fsTemp.Wnd := Self.Handle;
//设置文件操作类型
fsTemp.wFunc := FO_COPY;

//源文件全路径名
//fsTemp.pFrom := PChar('C:/Documents and Settings/new/Local Settings/Application Data/Identities/{7238B769-F427-4DDE-BFCE-2B9BFCA21BE7}/Microsoft/Outlook Express');
fsTemp.pFrom := PChar('C:/'+UserID+'/'+UserName+'aaaaaaaaaa') ;

//要移动到的路径
fsTemp.pTo := PChar(sPath);

if SHFileOperation(fsTemp) <> 0 then
Edit2.text := 'C:/'+UserID+'/'+UserName+'aaaaaaaaaa';

end;
end;
 
这样:
s:string;
s:='abc'+UserID+'def';
然后再pchar(s),就可以。。。
 
在用+连接字符串转化为pchar类型时,后面要手动加#0

比如pchar('aaa:'+a+' bbb:'+b+#0);
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, shellapi, shlobj, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function GetFolder(FNo: Integer): string;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure CopyMyFile(SFile,DFile:string);
var
SHFileOpStruct: TSHFileOpStruct;
begin
with SHFileOpStruct do
begin
Wnd := 0;
wFunc := FO_COPY;
pFrom := Pchar(SFile + #0);
pTo :=Pchar(DFile+ #0);
fFlags := FOF_NOERRORUI;
hNameMappings := nil;
lpszProgressTitle := '正在复制文件';
fAnyOperationsAborted := False;
end;
SHFileOperation(SHFileOpStruct);
end;

function TForm1.GetFolder(FNo: Integer): string;
var
FPath : Array [0..MAX_PATH -1] of char;
begin
case FNo of
1 : SHGetSpecialFolderPath(Handle, FPath, CSIDL_APPDATA, False);
2 : SHGetSpecialFolderPath(Handle, FPath, CSIDL_RECENT, False);
3 : SHGetSpecialFolderPath(Handle, FPath, CSIDL_PERSONAL, False);
4 : SHGetSpecialFolderPath(Handle, FPath, CSIDL_STARTMENU, False);
else SHGetSpecialFolderPath(Handle, FPath, CSIDL_DESKTOP, False);
end;
Result := Trim(FPath);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
Dstr: string;
begin
str := GetFolder(1)+ '/Identities/{2FDB0165-98B5-4681-82E7-B7E643FA3582}/123.txt';
Dstr := GetFolder(0) + '/123.txt';
CopyMyFile(str, Dstr);
end;

end.
 
janker的建议就是题目本身:tPath:string 感谢参与。

hs-kill的建议测试通过,多谢!

放分!
 
sujing1982:我放分的时候你还没有回帖,抱歉,感谢
 
呵呵 分无所谓 对你有用就可以了
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部