Change file permission in FTP using TIdFTP Delphi - delphi

How do I change the permissions of a file using TidFTP in Delphi?
with IdFTP1 do
begin
try
Connect;
ChangeDir(FTPDiretorio);
Put(FArquivo, NomeOnline);
chmod(' ');//640 HOW??????????????
Disconnect;
finally
FreeAndNil(IdFTP1);
end;
end;

CHMOD is not a standard FTP command. Some FTP servers implement it as a custom command, and others do not implement it at all. As such, you have to use the TIdFTP.Site() method to send it, on FTP servers you know support it, eg:
IdFTP.Site('CHMOD 640 filename');

Related

Delphi RAD Studio 10.2, unable to browse windows system folder using FileOpenDialog

I need to be able to enumerate the folders present under C:\Windows\system32\dns on a Windows server 2016 instance running Windows DNS server.
Having tried FindFirst()/FindNext() and getting no results, I built a quick VCL Forms App to understand what was happening. I have a TButton and a TEdit, and the button's OnClick is below:
procedure TForm1.Button1Click(Sender: TObject);
begin
FDir := 'C:\Windows\System32\';
with TFileOpenDialog.Create(nil) do
try
Title := 'Select Directory';
Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem];
OkButtonLabel := 'Select';
DefaultFolder := FDir;
FileName := FDir;
if Execute then
Edit1.Text := Filename;
finally
Free;
end;
end;
When I run this - either as Administrator, or normally, on the server - and try to browse to the folder C:\Windows\system32\dns\ in the FileOpenDialog, I get an error:
Windows can't find 'C:\Windows\system32\dns'. Check the spelling and try again.
However, I know the folder exists, and I can browse it using Windows Explorer on the server, so there must be an issue with the Delphi code, or the permissions the App is running under.
Please, can anyone suggest what I need to do to fix this?
Thanks to #SertacAkyuz for reminding me about file system redirection - trying to access %Windir%\system32 from a 32bit program will be redirected to %Windir%\SysWow64 which doesn't contain the dns folder.
You can use the virtual alias %Windir%\Sysnative to gain access to the actual system32 folder from a 32bit application, and that works for the above case. so browsing to %Windir%\sysnative\dns allows me to enumerate the folders correctly.

Why doesn't my code find any registry keys?

I want to list USB VIDs from the registry, and I wrote the following Delphi code:
procedure FindUSBvids(VIDs: TStrings);
var
Reg1: TRegistry;
begin
Reg1 := TRegistry.Create;
try
Reg1.RootKey := HKEY_LOCAL_MACHINE;
Reg1.OpenKey('System\CurrentControlSet\Enum\USB', False);
Reg1.GetKeyNames(VIDs);
finally
Reg1.Free;
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
FindUSBvids(Memo2.Lines);
end;
Unfortunately the Memo remains empty. I triple-checked the key, it exists and has a couple dozen subkeys. What am I doing wrong?
(Note: I'm not sure this is the right way to find USB VIDs, but that's not the point. It's just that I'm puzzled why the code doesn't produce any result.)
You are asking for write access to the registry key. Use OpenKeyReadOnly instead of OpenKey.
Are you running your application with elevated access rights as administrator or as a normal user.
When you are using OpenKey Delphi by default tries to access that registry key using Read and Write access.
Now on Windows XP this would be just fine but on Windows Vista and newer this can be blocked by UAC especially if you are trying to read registry contents from HKEY_LOCAL_MACHINE part because this part of the registry is protected and requires elevated privileges to be modified.
So you need either to launch your application with administrative rights or change the Access property to `KEY_READ'. I would recommend using the lather if you are only enumerating the USB devices.

Checking internet connection in delphi

How, i've made a program that uploads files on ftp server, the thing is that whenever i don't have access to the internet, it appears me an error, which stops running the program and says that i am not connected to the internet.
How do i make the program appear that message in a showmessage box, so that it doesn't stop running the program?
For example:
If internetconnection then
begin
end else showmessage ('You are not connected to the internet')
Please try code from this link
Ping
Also you can try to use free Internet Component Suite components that allows to implement test connection to your ftp server.
EDIT:
Since it was found that the author uses IdFTP (Indy) component for upload files on ftp server and my first answer was not good i will take courage to write as i think correct code for checking connection to the ftp server:
with IdFTP1 do begin
Host := ..;
Port := ..;
Username := ..;
Password := ..;
if Connected then Disconnect;
try
Connect;
ShowMessage('FTP IS Connected')
except
ShowMessage('FTP IS NOT Connected');
end;
end;

Delphi - Write/read files in a shared environment

I have some problems on files which are placed in a LAN: there is a single Delphi program (server) which should write some files, which can be only read by multiple Deplhi programs (clients).
I use these simple instructions in the server for writing (DataList is a TStrings):
Stream:=TFileStream.Create(filePath,fmOpenWrite or fmShareDenyWrite);
try
DataList.SaveToStream(Stream);
finally
Stream.Free;
end;
The clients check every 5 seconds if the file above is modified (by just checking the FileAge), and if modifications are occurred, they load the DataList in the following way:
try
Stream:=TFileStream.Create(filePath,fmOpenRead or fmShareDenyNone);
DataList.LoadFromStream(Stream);
finally
Stream.Free;
end;
Normally everything works perfectly, but sometimes it happens that the server or the client raise an exception because "the file is in use by other process".
I don't understand which is the problem: I tried many alternatives, but this can happen also with just the server and only one istance of the client running..
Any ideas?
Thanks!
By design, network file systems can't be trusted. At least, NFS (in Linux) and SMB (in Windows) have no proven lock feature: concurrent access is not safe.
You need to use a Client-Server protocol to ensure that shared data is safe. You can use TCP/IP, HTTP or any other mean.
I recommend using a true service implementation, like DataSnap, RemObjects or our Open Source mORMot.

How to programatically detect if the MySql Connector/ODBC is installed? (and install it if needed)

I am thinking of the client PC here. When someone comes to run my app, they may not have the MySql Connector/ODBC installed.
Now, I could just try to connect the information scheme (or mysql) database (which is preferable?) - but, if that fails, it might only mean that the database server is down. I could ping it, but it might be up and the MySql process not currently running.
I guess I could just install the connector anyway, even though it sounds sloppy, but not if doing so is going to noticably slow my app's start-up time.
What is the best approach?
When I do install, how do I do that from Delphi, given that the connector will be available on the d/b server, who's IP address is know?
Or ... at the moment my app is a simple .EXE, with no installer. Should I create an install which also installs the ODBC connector? (if so, can anyone recommend a good freeware install builder (with no adware or toolbar installs)?
I hope that this is clear. Please ask me if not.
Thanks.
To check the ODB drivers installed you must check this windows registry key
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
from there you can easily write a delphi function to detect if the MySQL ODBC driver is installed
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
Registry,
SysUtils;
function ODBC_DriverInstalled(const DriverName:string) : Boolean;
Var
Reg : TRegistry;
Providers: TStrings;
i : Integer;
begin
Reg:=TRegistry.Create;
Result:=False;
try
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers') then
begin
Providers:=TStringList.Create;
try
Reg.GetValueNames(Providers);
for i := 0 to Providers.Count-1 do
begin
if CompareText(DriverName,Providers[i])=0 then
begin
Result:=True;
Break;
end;
end;
finally
Providers.Free;
end;
end;
finally
Reg.Free;
end;
end;
begin
try
Writeln(ODBC_DriverInstalled('MySQL ODBC 5.1 Driver'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
To install you have several options one can be use inno setup (which can read the windows registry as well to check for the odbc driver) and then install the driver included inside of your setup file. also you can donwload the driver directly from your app using a simple HTTP GET passing one the download address located here
There is no need to install the MySQL connector. You just need to put the right dll (depending on the server version) in the same folder as your application executable. I would recommend an installer like InnoSetup. This will also let you add start menu shortcut etc.

Resources