QT-文件创建时间修改器
由于对某些文件需要进行时间修改,因此写了一个小的例子:
此程序具备拖拽文件功能,代码如下
#include "MainWindow.h"
void GetFileTime(const char* filePath)
{
FILETIME creationTime, lastAccessTime, lastWriteTime;
//HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
if (GetFileTime(hFile, &creationTime, &lastAccessTime, &lastWriteTime))
{
SYSTEMTIME stUTC, stLocal;
FileTimeToSystemTime(&creationTime, &stUTC);
SystemTimeToTzSpecificLocalTime(nullptr, &stUTC, &stLocal);
//std::wcout << L"File: " << filePath << std::endl;
//std::wcout << L"Creation Time: " << stLocal.wYear << L"-" << stLocal.wMonth << L"-" << stLocal.wDay
// << L" " << stLocal.wHour << L":" << stLocal.wMinute << L":" << stLocal.wSecond << std::endl;
}
CloseHandle(hFile);
}
}
void SetFileTime(const char* filePath, const FILETIME& newTime)
{
//HANDLE hFile = CreateFile(filePath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
HANDLE hFile = CreateFile(filePath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
SetFileTime(hFile, &newTime, &newTime, &newTime);
CloseHandle(hFile);
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setAcceptDrops(true);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnPushButton()));
}
MainWindow::~MainWindow()
{}
void MainWindow::dragEnterEvent(QDragEnterEvent * event)
{
if (event->mimeData()->hasUrls())
{
event->acceptProposedAction();
}
else
event->ignore();
}
void MainWindow::dropEvent(QDropEvent * event)
{
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls())
{
QList<QUrl> urlList = mimeData->urls();
urlList.size();
QStringList fileName;// = urlList.at(0).toLocalFile();
for (int i = 0; i < urlList.size(); i++)
{
fileName.push_back(urlList.at(i).toLocalFile());
qDebug() << urlList.at(i).toLocalFile();
ui.lineEdit->setText(urlList.at(i).toLocalFile());
}
}
}
void MainWindow::OnPushButton()
{
QString exePath = ui.lineEdit->text();
SYSTEMTIME st;
//GetSystemTime(&st);
QDateTime dateTime1 = ui.dateTimeEdit->dateTime().toLocalTime();
QDateTime dateTime = dateTime1.toUTC();
st.wYear = dateTime.date().year();
st.wMonth = dateTime.date().month();
st.wDay = dateTime.date().day();
st.wHour = dateTime.time().hour();
st.wMinute = dateTime.time().minute();
st.wSecond = dateTime.time().second();
st.wMilliseconds = dateTime.time().msec();
FILETIME newTime;
SystemTimeToFileTime(&st, &newTime);
SetFileTime(exePath.toLocal8Bit(), newTime);
GetFileTime(exePath.toLocal8Bit());
}
#pragma once
#include <QMainWindow>
#include "ui_MainWindow.h"
#include <QApplication>
#include <Windows.h>
#include <QDebug>
#include <QMimeData>
#include <QLineEdit>
#include <QDropEvent>
#include <QDragEnterEvent>
using namespace std;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
//拖拽外部文件读取arom功能
void dragEnterEvent(QDragEnterEvent *event); //拖拽进入事件
void dropEvent(QDropEvent *event); //放下事件
private slots:
void OnPushButton();
private:
Ui::MainWindowClass ui;
};