C++Builder How to use SameFileName method on Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How to use SameFileName method on Windows
By Yilmaz Yoru September 14, 2021

How can I check if two files are actually the same file? What is SameFileName method in C++? How can I compare file names based on the current Windows locale? Let’s answer these questions.

C++ Builder has a lot of specific methods in its Для просмотра ссылки Войди или Зарегистрируйся library that are included in VCL and FMX libraries. Some of these are grouped as a Для просмотра ссылки Войди или Зарегистрируйся that allow a user to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in the Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся,Для просмотра ссылки Войди или Зарегистрируйся libraries. These methods are easy to use and easy to get or set file path strings in that operating system. The functions can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files, or directories to see if they exist or not on a given path.

We can also check two file name strings if they are the same on the current locale. Let’s see how we do this.

What is the C++ SameFileName method?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System::SysUtils::SameFileName) is a SysUtils Method that compares file names based on the current locale. SameFileName compares two file names in the same way as Для просмотра ссылки Войди или Зарегистрируйся. SameFileName returns True if the file names match, False otherwise. Like Для просмотра ссылки Войди или Зарегистрируйся, SameFileName supports locales and MBCS strings; see the AnsiCompareFileName topic for more details.

The following table lists the parameters expected by this method:

NameMeaning
PathThe verified file or directory name
Note: that SameFileName method raises an exception if the given path contains invalid characters.

What is the syntax of the SameFileName method ?​

Here is the syntax for the SameFileName Method,
C++:
bool __fastcall SameFileName(const System::UnicodeString S1, const System::UnicodeString S2); // overload

Here is a simple example of using SameFileName Method​

C++:
bool same = SameFileName( L"MYIMAGE.jpg", L"myimage.jpg" );

Is there a more complete example of how to use the C++ SameFileName method?​

Here is the full C++ Builder VCL example,
C++:
#include <vcl.h>
 
#pragma hdrstop
 
#include "Include_Exclude_Path_Delimiters_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
   String filename = L"MYIMAGE.jpg";
   if ( SameFileName( filename, L"myimage.jpg") ) 
               Memo1->Lines->Add( L"Same file name");
 else   Memo1->Lines->Add( L"Not same file name");
}
Note that can use this method with the OpenDialog (TOpenDialog) or SaveDialog (TSaveDialog) components. For example you can check if OpenDialog1->FileName is exactly same as the file name that you want.