And again with a question! I need to change the date of creation of the file selected by the user. The program is compiled, but unfortunately the creation date does not change. The code adapted for the console is correctly executed and eventually changes the date the file was created ... A piece of code below. Tell me what is the reason?

string naka = SystemToStl (nama); // name of the selected file, obtained using OpenFileDialog

LPCSTR lpMyString = naka.c_str(); SYSTEMTIME lf; FILETIME ft; OFSTRUCT of; HANDLE hFile=(HANDLE)OpenFile(lpMyString, &of, OF_CREATE); if(!hFile) { MessageBox::Show("ERRROR","Справка", MessageBoxButtons::OK);} ::GetFileTime(hFile, &ft, NULL, NULL); FileTimeToSystemTime(&ft, &lf); String^ strok = ""; unsigned short vert = 0; // WORD years, mesiac, den, chas, minuta, secunda; vert = Convert::ToInt32(textBox2->Text); lf.wYear=vert; vert = Convert::ToInt32(textBox3->Text); lf.wDay=vert; vert = Convert::ToInt32(textBox4->Text); lf.wMonth=vert; vert = Convert::ToInt32(textBox5->Text); lf.wHour=vert; vert = Convert::ToInt32(textBox6->Text); lf.wMinute=vert; vert = Convert::ToInt32(textBox7 ->Text); lf.wSecond=vert; SystemTimeToFileTime(&lf, &ft); SetFileTime(hFile, &ft, NULL, NULL); CloseHandle(hFile); 
  • To get started, check the return code at SetFileTime . See docs.microsoft.com/en-us/windows/desktop/api/fileapi/… - Herman Borisov
  • And also check what SystemTimeToFileTime returns, there may also be a problem. - freim
  • Can use stat() function? - NewView
  • @HermanBorisov, the Set function and GetFileTime, return not 0, but 6, i.e. work probably correctly ... But SystemTimeToFileTime returned 0 .. Do you think there may be a problem in initializing unsigned short vert instead of WORD (like this is the same thing) ... I can’t see the error statistics .. - Anna
  • @freim, SystemTimeToFileTime returned 0 .. Do you think a problem is possible in the initialization of unsigned short vert instead of WORD (like this is the same thing) - Anna

2 answers 2

Judging by the code in question, you have mixed native C ++ with managed C ++ / CLI (and the WinForms framework is used).

Let's simplify everything using the capabilities of the .NET Framework.

 OpenFileDialog ^ ofd = gcnew OpenFileDialog(); if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) { String ^ name = ofd->FileName; FileInfo ^ info = gcnew FileInfo(name); info->CreationTime = dateTimePicker->Value; } 

From the open dialog, we get the file name, create a FileInfo object. Next, simply set the CreationTime property to a new time. Everything!

In this example, I take the time from the DateTimePicker component (it is assumed that it is on the form). I suppose its use is more convenient than a few TextBox . But if you want, you can get numbers from textboxes and create an instance of DateTime from them.

Again, to get numbers, I would use NumericUpDown instead of text boxes.

  • Thank! Works!!! - Anna

Most likely the problem is in the function OpenFile . This function can only be used with 16-bit versions of Windows (if you suddenly find an IBM PC AT with Windows 3.1), and it returns not HANDLE , but some HFILE (which you force into HANDLE ). Try replacing this function with such a call:

 HANDLE hFile = ::CreateFile(lpMyString, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); 
  • Last question ... Not anymore .. But error: error C2664: 'CreateFileW': cannot convert parameter 1 from 'LPCSTR' to 'LPCWSTR' ... Do I need to convert? - Anna
  • one
    @Anna, your program is compiled in Unicode string mode, that is, CreateFile waiting for the name of the file in Unicode. And your lpMyString is a regular string. Either define it as LPCWSTR (by the way, why is it not Unicode? Not clear), or call CreateFileA . - freim
  • Yes, I called CreateFileA, but Get returns 0 again ............. AAA! Why does the console work ?! - Anna
  • one
    @Anna, and CreateFile normally works? It is necessary to compare the returned handle with INVALID_HANDLE_VALUE . If it’s equal, call GetLastError and see what it doesn’t like. What file path? - freim
  • Thank you for coming to the rescue! If I specify the path to the LPCSTR file in the program lpMyString = "D: \ mmmyfile.txt"; then the program executes correctly, creates a file and sets the creation date specified by the user ... It turns out and if the user in textBox has specified the file name and path to it ... But the problem is that I need the user to select an existing file and changed the date of its creation .. - Anna