分享一个好用的文件测试工具:filelocker
官网:https://jensscheffler.de/filelocker
具体使用方法:
Usage:
FileLocker [/T LockTime] [/I] [/K] [/Q] file [file...]
/T LockTime Time in milliseconds to lock the file
/I Infinite locking until process is killed (default)
/K Lock file until key is pressed
/Q Be quiet.
例如:
D:\>FileLocker.exe /I 122.txt
FileLocker 1.0 - Tool to set a lock on file(s)
Locking file 122.txt ... OK!
Sleeping forever... please kill me to unlock files!
源代码非常简单:
// FileLocker.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include "Windows.h"
static bool quiet = FALSE;
static bool keypress = FALSE;
static int sleeptime = -1;
void printHeader()
{
if (!quiet)
{
printf("FileLocker 1.0 - Tool to set a lock on file(s)\n\n");
}
}
void printUsage()
{
printf("Usage:\n");
printf("FileLocker [/T LockTime] [/I] [/K] [/Q] file [file...]\n\n");
printf("/T LockTime Time in milliseconds to lock the file\n");
printf("/I Infinite locking until process is killed (default)\n");
printf("/K Lock file until key is pressed\n");
printf("/Q Be quiet.\n\n");
}
/**
* File Locker is a small tool to lock a file on the hard drive
* to block file access from other applications.
* This program was mainly implemented for testing purposes
*/
int main(int argc, char* argv[])
{
// step 1: parse arguments
int pos = 1;
while (pos < argc && (argv[pos][0] == '/' || argv[pos][0] == '-'))
{
if (strlen(argv[pos]) != 2)
{
printHeader();
fprintf(stderr, "ERROR: Unknown argument: %s\n\n", argv[pos]);
printUsage();
return -1;
}
switch (argv[pos][1])
{
case 't':
case 'T':
pos++;
if (pos >= argc)
{
printHeader();
fprintf(stderr, "ERROR: LockTime argument missing\n\n");
printUsage();
return -1;
}
sleeptime = atoi(argv[pos]);
if (sleeptime <= 0)
{
printHeader();
fprintf(stderr, "ERROR: Invalid LockTime specified: %s\n\n", argv[pos]);
printUsage();
return -1;
}
break;
case 'i':
case 'I':
// ignore this, it is default anyway
break;
case 'k':
case 'K':
keypress = TRUE;
break;
case 'q':
case 'Q':
quiet = TRUE;
break;
case '?':
printHeader();
printUsage();
return 0;
break;
default:
printHeader();
fprintf(stderr, "ERROR: Unknown argument: %s\n\n", argv[pos]);
printUsage();
return -1;
break;
}
pos++;
}
if (pos == argc)
{
printHeader();
fprintf(stderr, "ERROR: No file specified for locking\n\n");
printUsage();
return 0;
}
// step 2: Lock file(s)
printHeader();
HANDLE *files = new HANDLE[argc - pos];
for (int i = pos; i < argc; i++)
{
if (!quiet)
{
printf("Locking file %s ...", argv[i]);
}
files[i] = CreateFile(argv[i], GENERIC_ALL, 0x0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (files[i] == INVALID_HANDLE_VALUE)
{
// file cannot be opened, generate error message
fprintf(stderr, "\nERROR: Locking of file failed: %s\n", argv[i]);
int errCode = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
0,
(LPTSTR) &lpMsgBuf,
0,
NULL);
fprintf(stderr, " Errorcode %i: %s", errCode, lpMsgBuf);
LocalFree(lpMsgBuf);
// unlock locked files and exit
for (int j = pos; j < i; j++)
{
CloseHandle(files[j]);
}
return -2;
}
if (!quiet)
{
printf(" OK!\n");
}
}
// step 3: Wait for end of locking...
if (keypress)
{
if (!quiet) {
printf("Press key to unlock and exit");
}
getchar();
}
else if (sleeptime > 0) {
if (!quiet) {
printf("Sleeping for %i milliseconds...\n", sleeptime);
}
Sleep(sleeptime);
}
else
{
if (!quiet) {
printf("Sleeping forever... please kill me to unlock files!\n");
}
while (TRUE)
{
Sleep(100000);
}
}
// cleanup: remove locks
for (int c = pos; c < argc; c++)
{
if (!quiet)
{
printf("Unlocking file %s\n", argv[c]);
}
CloseHandle(files[c]);
}
return 0;
}
可以看到,代码非常简单,但是这个作者把他封装的非常好,非常方便使用。
如果自己写测试代码的话,可能就写了一个CreateFile就完了,不会再继续完善,然后后面可能代码又不知道放哪里了,又要重新写一个。
这个老外也是比较用心的,赞!
也要收到点启发,后续如果有一些好的自测工具,也要想办法做好,然后分享给别人用。
如果在网上找到了现成的,就直接拿来使用,如果没有,就自己写一个好一点的工具。
目的也是为了在日常开发中,自测的时候使用,也是为了提高效率。