文件打开检测 [英] File open with detection
本文介绍了文件打开检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,如何检测我的程序是否通过文件打开
例如,如果我打开一个txt,它会在记事本中打开
如何检测程序中的开头?
解决方案
public bool IsFileinOpen(FileInfo文件)
{
FileStream stream = 空跨度>;
try
{
stream = file.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.None) ;
}
catch (IOException)
{
// 该文件不可用,因为它是:
// 仍然被写入
// 或正由另一个线程处理
// 或不存在(已经处理完毕)
返回 true ;
}
最后
{
如果(流!= null )
stream.Close();
}
返回 false ;
}
FileInfo fileObj = new FileInfo( C:\\yourfile.txt跨度>);
bool iSOpen = IsFileinOpen(fileObj);
如果此方法返回false,则文件不是否则它用于任何其他程序。
这是一个非常高级的主题,你需要实现一个钩子,你必须使用本机代码。例如,请参阅:API挂钩显示 [ ^ ]。
我设法修复我自己
这是解决方案:
加入(不要删除)
public Form1()
{
InitializeComponent();
}
--->
public Form1(字符串来电)
{
InitializeComponent();
// Caller =文件的路径。
}
Program.cs:
static void Main(string [] args)
并且:
if (args.Count()== 1 )
Application.Run( new Form1(args [ 0 ]));
else
Application.Run( new Form1());
Hi there, how do i detect if my program is opened trough a file
For example, if i open a txt it opens in notepad
How do i detect the opening in my program?
解决方案
public bool IsFileinOpen(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } return false; } FileInfo fileObj = new FileInfo("C:\\yourfile.txt"); bool iSOpen = IsFileinOpen(fileObj);
If this method returns false then file is not used otherwise it is used in any other program.
That's a pretty advanced topic, you need to implement a hook, and you have to do it with native code. See, for instance: "API hooking revealed"[^].
I managed to fix it myself
Here's the solution:
Add under (Dont remove)
public Form1()
{
InitializeComponent();
}
--->
public Form1(string caller)
{
InitializeComponent();
//Caller = the path to the file.
}
Program.cs:
static void Main(string [] args)
And:
if (args.Count() == 1) Application.Run(new Form1(args[0])); else Application.Run(new Form1());
这篇关于文件打开检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文