windows下文件占用测试工具

分享一个好用的文件测试工具: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就完了,不会再继续完善,然后后面可能代码又不知道放哪里了,又要重新写一个。

这个老外也是比较用心的,赞!

也要收到点启发,后续如果有一些好的自测工具,也要想办法做好,然后分享给别人用。

如果在网上找到了现成的,就直接拿来使用,如果没有,就自己写一个好一点的工具。

目的也是为了在日常开发中,自测的时候使用,也是为了提高效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值