打开文件时出错,请指教

ltolll 2003-04-24 12:05:36
在网上得到一篇代码写的是:得到局域网内所有电脑名。
三个控件:
1个button
1个memo
1个timer,enable属性先设为false

unit1中代码如下:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Timer1: TTimer;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
VAR
command:string ;
comline: pchar ;
begin
command:='COMMAND.COM /C net view>c:\123ip.txt';
comline:=pchar(command);
WinExec(comline, sw_hide);
timer1.Enabled:=true;

end;

procedure TForm1.Timer1Timer(Sender: TObject);
var f:tstringlist;
ll,ii:integer;
s1,s:string;
b:bool;
begin
f:=tstringlist.Create;
try
f.LoadFromFile('c:\123ip.txt');
except
end;
if f.Count>10 then begin
memo1.Clear;
memo1.lines.add(f.text);
timer1.Enabled:=false;
f.Free;
{去掉多余的行和'\\'}
for ll:=memo1.lines.count-1 downto 0 do begin
if strpos(pchar(memo1.lines.strings[ll]),'\\')=nil then
memo1.lines.delete(ll)
else
// memo1.lines.Strings[ll]:=delete(memo1.lines.Strings[ll],'\\');
{自编1个去'\\'的函数}
end;
timer1.enabled:=False;
end;

end;

end.

//////////////////
运行时点接钮报错
为验证是否是打开文件时代码错误又建一工程:
1个button
1个memo

unit1中代码如下:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var f:tstringlist;
ll,ii:integer;
s1,s:string;
b:bool;
begin
f:=tstringlist.Create;
try
f.LoadFromFile('c:\123ip.txt');//此处也可放其它已存在的文件。
except
end;
if f.Count>10 then begin
memo1.Clear;
memo1.lines.add(f.text);
f.Free;
{去掉多余的行和'\\'}
for ll:=memo1.lines.count-1 downto 0 do begin
if strpos(pchar(memo1.lines.strings[ll]),'\\')=nil then
memo1.lines.delete(ll)
else
//memo1.lines.Strings[ll]:=delxxx(memo1.lines.Strings[ll]);
{自编1个去'\\'的函数}
end;

end;

end;

end.

////////////
运行时点接钮,正常运行
代码都差不多
不知是哪里不对,请指教。
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hezchuan168 2003-04-29
  • 打赏
  • 举报
回复
兄弟,帮你找出原因了,把f:=tstringlist.Create;
放到try里面就没有出错了,哈!这也是没有办法的办法
hezchuan168 2003-04-29
  • 打赏
  • 举报
回复
我试做了一下,运行提示:
f.LoadFromFile('c:\123ip.txt');错误
ltolll 2003-04-29
  • 打赏
  • 举报
回复
谢谢chiprobot——泪!

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧