Solved

Networking and delphi

Posted on 2004-04-02
7
1,062 Views
Last Modified: 2013-11-15
i'm just curious If my computer had access to other computers on a network, where would the info be in the registry and can i write a program in delphi that will copy files to those computers?
0
Comment
Question by:herd_bone
  • 5
7 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10749627

  but you can read the network status from the NETINFOSTRUCT.

var lpProvider : Pchar;
    lpNetInfoStruct : _NETINFOSTRUCT;
begin
  WNetGetNetworkInformation(lpProvider,lpNetInfoStruct);
  if lpNetInfoStruct.dwStatus = ERROR_NO_NETWORK then
    // sorry, no network

However, even something simpler is a path  - if you know a path on the network, you can test for files.
If that path exists, there is a network.

Example (utilizing UNC paths)

if fileExists('\\Gaming-station1\shareddocs\Test.txt') then
 ShowMessage('Network exists!');

where, Gaming-station1 is a name of a computer on the network

Also,

Yes, you can write a program to copy files to another computer. This is done all the time, especially in a backup style application.

See this link for an example of copying files (lots of good options) in a Backup application

http://www.delphi.about.com/library/weekly/aa033004a.htm


You could also use the win32 call to

CopyFile(lpExistingFileName: PAnsiChar; lpNewFileName: PAnsiChar; bFailIfExists: LoongBool);

Again, using the UNC path to the folder on the network

Shane
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 250 total points
ID: 10755429
Hi herd_bone,

download from        http://www.geocities.com/esoftbg/
                 file        Q_20937024.exe.zip
I can not taste this   Q_20937024.exe because I have not Network at my home. If it works well and you are interested, I will send the source code.

emil
0
 

Author Comment

by:herd_bone
ID: 10798576
What does it do, basically i need my program to be network aware.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 12

Expert Comment

by:esoftbg
ID: 10807548
simply:
1.Select a source folder (top-left component);
2.Select a source fсle (top-псааве component);
3.Select a target network folder (bottom-left component);
4.Press Copy button.
If it works, the selected file will be copied into the selected network folder.

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10807558
>2.Select a source fсle (top-псааве component);
to be readed as:
2.Select a source file (top-middle component);
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10867261
This is not a new answer of this question. It is the code of the accepted one above. Let it be here if my site would be destroyed in the future for any reason.

Hi herd_bone, if you share your e-mail address I could sent into it the source code which is below as zip file.

unit Unit_Q_20942205;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,    Graphics, Controls,
  IdGlobal,Dialogs,  Forms,    ComCtrls, ShellCtrls, Buttons,  StdCtrls;

type
  TForm1 = class(TForm)
    ShellListView: TShellListView;
    ShellTreeView: TShellTreeView;
    spbCopy: TSpeedButton;
    Memo: TMemo;
    stv: TShellTreeView;
    lblSourceFolder: TLabel;
    lblSourcefile: TLabel;
    lblResult: TLabel;
    spbClear: TSpeedButton;
    lblTargetfolder: TLabel;
    procedure spbCopyClick(Sender: TObject);
    procedure spbClearClick(Sender: TObject);
  private        { Private declarations }
  public         { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.spbCopyClick(Sender: TObject);
var
  B:      Boolean;
  S:      string;
  T:      string;
  R:      string;
begin
  R := 'File Copy was NOT Successful';
  B := False;
  try
    try
      S := ShellListView.SelectedFolder.PathName;
      if not ShellListView.SelectedFolder.IsFolder then
      begin
        T := ShellTreeView.SelectedFolder.PathName;
        T := T + '\' + ExtractFileName(S);
        B := CopyFileTo(S, T);
      end;
    except
      on e : Exception do
      begin
        B := False;
        R := e.Message;
      end;
    end;
  finally
    if B then
    begin
      Memo.Lines.Add(S);
      Memo.Lines.Add('was SUCCESSFULY COPIED to ');
      Memo.Lines.Add(T);
      Memo.Lines.Add('--------------------------------------------------------------------------------------------------------------------------------');
    end
    else
    begin
      Memo.Lines.Add(R);
      Memo.Lines.Add('--------------------------------------------------------------------------------------------------------------------------------');
    end
  end;
end;

procedure TForm1.spbClearClick(Sender: TObject);
begin
  Memo.Clear;
end;

end.

//................

object Form1: TForm1
  Left = 216
  Top = 89
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Networking and Delphi'
  ClientHeight = 606
  ClientWidth = 760
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefaultPosOnly
  PixelsPerInch = 96
  TextHeight = 13
  object spbCopy: TSpeedButton
    Left = 8
    Top = 288
    Width = 320
    Height = 32
    Caption = '    COPY selected source to selected destination    '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    Glyph.Data = {
      D6030000424DD603000000000000760000002800000048000000180000000100
      0400000000006003000000000000000000001000000000000000000000000000
      8000008000000080800080000000800080008080000080808000C0C0C0000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00888888888888
      8888888888888888888888888888888888888888888888888888888888888888
      8888888888888888888888888888888888888888888888888888888888888888
      8888888888888888888888888888888888888888888888888888888888888888
      8888888888888888888888880778888888888888888888F77888888888888888
      88888888888888888888888888888880007788888888888888888FFF77888888
      8888888888888888888888888888888888888800C0077888888888888888FF8F
      F7788888888888888888880778888888888888888888800CCC00778888888888
      888FF888FF77888888888888888880007788888888888888888800CCCCC00778
      8888888888FF88888FF7788888888888888800C0077888888888888888800CCC
      CCCC0077888888888FF8888888FF77888888888888800CCC0077888888888888
      8800CCCCCCCCC00778888888FF888888888FF778888888888800CCCCC0077888
      88888888800CCCCCCCCCCC007788888FF88888888888FF7788888888800CCCCC
      CC0077888888888800CCCCCCCCCCCCC0077888FF8888888888888FF778888888
      00CCCCCCCCC0077888888880000000CCCCC0000000778FFFFFFF88888FFFFFFF
      778888800CCCCCCCCCCC007788888888888880CCCCC0788888888888888F8888
      8F78888888888800CCCCCCCCCCCCC00778888888888880CCCCC0788888888888
      888F88888F788888888880000000CCCCC000000077888888888880CCCCC07888
      88888888888F88888F788888888888888880CCCCC078888888888888888880CC
      CCC0788888888888888F88888F788888888888888880CCCCC078888888888888
      888880CCCCC0788888888888888F88888F788888888888888880CCCCC0788888
      88888888888880CCCCC0788888888888888F88888F788888888888888880CCCC
      C078888888888888888880000000888888888888888FFFFFFF88888888888888
      8880CCCCC0788888888888888888888888888888888888888888888888888888
      888888888880CCCCC07888888888888888888888888888888888888888888888
      8888888888888888888000000088888888888888888888888888888888888888
      8888888888888888888888888888888888888888888888888888888888888888
      8888888888888888888888888888888888888888888888888888}
    NumGlyphs = 3
    ParentFont = False
    OnClick = spbCopyClick
  end
  object lblSourceFolder: TLabel
    Left = 12
    Top = 8
    Width = 63
    Height = 13
    Caption = 'Source folder'
  end
  object lblSourcefile: TLabel
    Left = 188
    Top = 8
    Width = 50
    Height = 13
    Caption = 'Source file'
  end
  object lblResult: TLabel
    Left = 332
    Top = 8
    Width = 33
    Height = 13
    Caption = 'Result:'
  end
  object spbClear: TSpeedButton
    Left = 384
    Top = 2
    Width = 128
    Height = 22
    Caption = 'Clear Results'
    OnClick = spbClearClick
  end
  object lblTargetfolder: TLabel
    Left = 12
    Top = 324
    Width = 82
    Height = 13
    Caption = 'Destination folder'
  end
  object ShellListView: TShellListView
    Left = 184
    Top = 24
    Width = 144
    Height = 256
    ObjectTypes = [otFolders, otNonFolders]
    Root = 'rfMyComputer'
    ShellTreeView = stv
    Sorted = True
    ReadOnly = False
    HideSelection = False
    TabOrder = 0
    ViewStyle = vsList
  end
  object ShellTreeView: TShellTreeView
    Left = 8
    Top = 340
    Width = 320
    Height = 256
    ObjectTypes = [otFolders]
    Root = 'rfNetwork'
    UseShellImages = True
    AutoRefresh = False
    Indent = 19
    ParentColor = False
    RightClickSelect = True
    ShowRoot = False
    TabOrder = 1
  end
  object Memo: TMemo
    Left = 328
    Top = 24
    Width = 416
    Height = 572
    ReadOnly = True
    TabOrder = 2
  end
  object stv: TShellTreeView
    Left = 8
    Top = 24
    Width = 176
    Height = 256
    ObjectTypes = [otFolders]
    Root = 'rfMyComputer'
    ShellListView = ShellListView
    UseShellImages = True
    AutoRefresh = False
    Indent = 19
    ParentColor = False
    RightClickSelect = True
    ShowRoot = False
    TabOrder = 3
  end
end
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10872289
In addition there are two possible (not tested trough LAN) ways to copy File and Folder:

procedure TForm1.Button_Lan_CopyFileClick(Sender: TObject);
var
  S:      string;
  T:      string;
  R:      string;
begin
  S := 'F:\Q_20932619.txt';
  R := '\\Emo\C:\Q_20932619.txt';
  case CopyFileTo(S, R) of
    True:  T := 'was SUCCESSFULY COPIED to ';
    False: T := 'was NOT SUCCESSFULY COPIED to ';
  end;
  Memo.Lines.Add(S);
  Memo.Lines.Add(T);
  Memo.Lines.Add(R);
  Memo.Lines.Add('--------------------------------------------------------------------------------------------------------------------------------');
end;

function  Copy_Dir(Dir, Source, Target: string): Boolean;
var
  B:      Boolean;
  T:      TSHFileOpStruct;
begin
  B := False;
  if not DirectoryExists(Target+Dir) then
    ForceDirectories(Target);
  try
    Fillchar(T, SizeOf(T), #0);
    with T do
    begin
      Wnd := 0;
      wFunc := FO_COPY;
      pFrom := PChar(Source+Dir + #0#0);
      pTo := PChar(Target+Dir + #0#0);
      fFlags := FOF_ALLOWUNDO or FOF_SILENT or FOF_NOCONFIRMATION;
    end;
    Application.ProcessMessages;
    B := (SHFileOperation(T)=0);
  finally
    Result := B;
  end;
end;

procedure TForm1.Button_Lan_CopyFolderClick(Sender: TObject);
var
  S:      string;
  T:      string;
  R:      string;
  Dir:    string;
begin
  S := 'F:';
  R := '\\emo\C:';
  Dir := '\SFX';
  case Copy_Dir(Dir, S, R) of
    True:  T := 'was SUCCESSFULY COPIED to ';
    False: T := 'was NOT SUCCESSFULY COPIED to ';
  end;
  Memo.Lines.Add(S+Dir);
  Memo.Lines.Add(T);
  Memo.Lines.Add(R+Dir);
  Memo.Lines.Add('--------------------------------------------------------------------------------------------------------------------------------');
end;
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now