473,573 Members | 4,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to detect when the external programm closes the file?

Ana
Hi!
I have problems with the following scenario:
My application is developed using C# under .NET. It must run on all Windows
versions starting from Windows 98.
The user must open different documents (txt, MS Office files, pdf,
pictures,…) from inside my app. It must start the file with the adequate
external program (Notepad, MS Office programs, Acrobat Reader, some Picture
viewer,... ) and be notified when this programs closes the document.
I have tried using various algorithms described on the newsgroups, like
using WaitForSingleOb ject and "Shell and Wait", but the problem is that it
does not work when the program being called is already started.
For example, when Outlook is started, the winword.exe process is running by
default. Opening a MS Word document from inside my app does not start a new
winword.exe process. Instead it just opens my document in a new Word window.
Because of that, starting winword.exe from my app, does not actually start
it, and I can't therefore wait for the process to end or check exit code or
do something similar.
I also tried enumerating the running processes, using
System.Manageme nt..ManagementC lass methods, but I can’t get the information
about the files that a program has opened, just about it’s related DLLs and
EXEs (process.GetRel ated("CIM_DataF ile")).

I would like either to be notified when the file is closed or check, by
using some kind of a file handle, if my file is still opened.
Does anybody have a suggestion how to do it?

Thanx, ANA.
Nov 16 '05 #1
6 5140
Hi,

I believe there is someway to know which application has a file opened,
take a look at sysinternals.co m's utilities, they have one that does this
,it shows you the file opened ni the system and what program is using them.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ana" <An*@discussion s.microsoft.com > wrote in message
news:E3******** *************** ***********@mic rosoft.com...
Hi!
I have problems with the following scenario:
My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98.
The user must open different documents (txt, MS Office files, pdf,
pictures,.) from inside my app. It must start the file with the adequate
external program (Notepad, MS Office programs, Acrobat Reader, some Picture viewer,... ) and be notified when this programs closes the document.
I have tried using various algorithms described on the newsgroups, like
using WaitForSingleOb ject and "Shell and Wait", but the problem is that it
does not work when the program being called is already started.
For example, when Outlook is started, the winword.exe process is running by default. Opening a MS Word document from inside my app does not start a new winword.exe process. Instead it just opens my document in a new Word window. Because of that, starting winword.exe from my app, does not actually start
it, and I can't therefore wait for the process to end or check exit code or do something similar.
I also tried enumerating the running processes, using
System.Manageme nt..ManagementC lass methods, but I can't get the information about the files that a program has opened, just about it's related DLLs and EXEs (process.GetRel ated("CIM_DataF ile")).

I would like either to be notified when the file is closed or check, by
using some kind of a file handle, if my file is still opened.
Does anybody have a suggestion how to do it?

Thanx, ANA.

Nov 16 '05 #2
Ana,

The only way I can think of to do this would be to have a thread that
would constantly try and open the file without sharing it, and by not
allowing other processes to read it. You can do this by calling the
CreateFileEx API directly and checking the return value, or by trying to
create a FileStream which does the same thing and catch the exception that
is thrown when it can't be created. Then, you can fire an event from this
thread that constantly polls to try and do this.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ana" <An*@discussion s.microsoft.com > wrote in message
news:E3******** *************** ***********@mic rosoft.com...
Hi!
I have problems with the following scenario:
My application is developed using C# under .NET. It must run on all
Windows
versions starting from Windows 98.
The user must open different documents (txt, MS Office files, pdf,
pictures,.) from inside my app. It must start the file with the adequate
external program (Notepad, MS Office programs, Acrobat Reader, some
Picture
viewer,... ) and be notified when this programs closes the document.
I have tried using various algorithms described on the newsgroups, like
using WaitForSingleOb ject and "Shell and Wait", but the problem is that it
does not work when the program being called is already started.
For example, when Outlook is started, the winword.exe process is running
by
default. Opening a MS Word document from inside my app does not start a
new
winword.exe process. Instead it just opens my document in a new Word
window.
Because of that, starting winword.exe from my app, does not actually start
it, and I can't therefore wait for the process to end or check exit code
or
do something similar.
I also tried enumerating the running processes, using
System.Manageme nt..ManagementC lass methods, but I can't get the
information
about the files that a program has opened, just about it's related DLLs
and
EXEs (process.GetRel ated("CIM_DataF ile")).

I would like either to be notified when the file is closed or check, by
using some kind of a file handle, if my file is still opened.
Does anybody have a suggestion how to do it?

Thanx, ANA.

Nov 16 '05 #3
Ana
Hi!
Thanks for the answer.
I already tried to find the solution by searching through the public code
that can be downloaded from sysinternals.co m, but without success.
Their program “Processor Explorer” really shows for each running app which
files it has opened or is related to.
But there is no explanation, how they find this kind of info.
Ana.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

I believe there is someway to know which application has a file opened,
take a look at sysinternals.co m's utilities, they have one that does this
,it shows you the file opened ni the system and what program is using them.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ana" <An*@discussion s.microsoft.com > wrote in message
news:E3******** *************** ***********@mic rosoft.com...
Hi!
I have problems with the following scenario:
My application is developed using C# under .NET. It must run on all

Windows
versions starting from Windows 98.
The user must open different documents (txt, MS Office files, pdf,
pictures,.) from inside my app. It must start the file with the adequate
external program (Notepad, MS Office programs, Acrobat Reader, some

Picture
viewer,... ) and be notified when this programs closes the document.
I have tried using various algorithms described on the newsgroups, like
using WaitForSingleOb ject and "Shell and Wait", but the problem is that it
does not work when the program being called is already started.
For example, when Outlook is started, the winword.exe process is running

by
default. Opening a MS Word document from inside my app does not start a

new
winword.exe process. Instead it just opens my document in a new Word

window.
Because of that, starting winword.exe from my app, does not actually start
it, and I can't therefore wait for the process to end or check exit code

or
do something similar.
I also tried enumerating the running processes, using
System.Manageme nt..ManagementC lass methods, but I can't get the

information
about the files that a program has opened, just about it's related DLLs

and
EXEs (process.GetRel ated("CIM_DataF ile")).

I would like either to be notified when the file is closed or check, by
using some kind of a file handle, if my file is still opened.
Does anybody have a suggestion how to do it?

Thanx, ANA.


Nov 16 '05 #4
Hi, Ana.
In win32 exists a function (NetEnumFile) that can build a list of opened
files in a specified folder.

I' dont konw if fw include something like this function (like mutex and
critical section implementation. .).

HTH

Alberto Salvati

--
Alberto Salvati
An italian delphi developer that are studing c# & .Net

Nov 16 '05 #5
Ana
Hi!
Thanks for the answer.

I thought about this solution, but it doesn’t suite the rest of the
application logic because of the following;

The documents being opened, must be decrypted and copied from a secure
location to some temporary folder, and opened from there. When the user
finishes working with a document, it must be encrypted, copied back to the
secure location, and the temporary copy must be naturally deleted.
I wanted to use this “notification when a file closes” to encrypt back the
file and delete it from the temporary folder. This was supposed to run in a
background thread, some kind of “garbage collector”.

I am not sure if the solution You suggested can guarantee the
“transactiona l” sequence of operations. If I get the exclusive access to a
file in one moment (so in that moment nobody else is using the file), release
the file and try to delete it in the next moment, what is the guarantee that
somebody else will not try to open this document in the meantime.

For example, the user started opening a file, and this operation lasts for
some time. The file is still not blocked, my “garbage collector” sees that
and deletes the file. In the meantime the slow application decided that it
will finally open the file and it does not find it. What do You think about
such scenarios. Are they possible? Do I have to worry about such things?

The solution of deleting all files at application shutdown is also not good
enough, because the files are “secret”, and it would be much better to secure
them immediately after the user finishes using them.

Ana.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Ana,

The only way I can think of to do this would be to have a thread that
would constantly try and open the file without sharing it, and by not
allowing other processes to read it. You can do this by calling the
CreateFileEx API directly and checking the return value, or by trying to
create a FileStream which does the same thing and catch the exception that
is thrown when it can't be created. Then, you can fire an event from this
thread that constantly polls to try and do this.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ana" <An*@discussion s.microsoft.com > wrote in message
news:E3******** *************** ***********@mic rosoft.com...
Hi!
I have problems with the following scenario:
My application is developed using C# under .NET. It must run on all
Windows
versions starting from Windows 98.
The user must open different documents (txt, MS Office files, pdf,
pictures,.) from inside my app. It must start the file with the adequate
external program (Notepad, MS Office programs, Acrobat Reader, some
Picture
viewer,... ) and be notified when this programs closes the document.
I have tried using various algorithms described on the newsgroups, like
using WaitForSingleOb ject and "Shell and Wait", but the problem is that it
does not work when the program being called is already started.
For example, when Outlook is started, the winword.exe process is running
by
default. Opening a MS Word document from inside my app does not start a
new
winword.exe process. Instead it just opens my document in a new Word
window.
Because of that, starting winword.exe from my app, does not actually start
it, and I can't therefore wait for the process to end or check exit code
or
do something similar.
I also tried enumerating the running processes, using
System.Manageme nt..ManagementC lass methods, but I can't get the
information
about the files that a program has opened, just about it's related DLLs
and
EXEs (process.GetRel ated("CIM_DataF ile")).

I would like either to be notified when the file is closed or check, by
using some kind of a file handle, if my file is still opened.
Does anybody have a suggestion how to do it?

Thanx, ANA.


Nov 16 '05 #6
When you open the file to decrypt it, you are obviously writing to it, so it
can't be deleted then. *Before* you close the handle of whatever is writing
to it, you open it with your garbage collector thread for read access, but
with FileShare.ReadW rite. It will fail if you try to use any other file
share mode, saying that it is being used by another process, which it is -
the decryptor, writing to it. It is fine for your garbage collector to have
a readonly handle to it as long as it has got the correct file share mode,
read write (this means it allows another process to write to it). But -
when your decryptor finishes writing to it and closes its handle, it still
can't be deleted, because your garbage collector's reading it. Then, when
you're ready to delete it, your deleter thread can come along and open it
for read write access, because the garbage collector's still only holding on
to it with read write share. You can then either set every byte in it to
zero, or just be content with closing the handle and then in the very next
call deleting it.
"Ana" <An*@discussion s.microsoft.com > wrote in message
news:A8******** *************** ***********@mic rosoft.com...
Hi!
Thanks for the answer.

I thought about this solution, but it doesn't suite the rest of the
application logic because of the following;

The documents being opened, must be decrypted and copied from a secure
location to some temporary folder, and opened from there. When the user
finishes working with a document, it must be encrypted, copied back to the
secure location, and the temporary copy must be naturally deleted.
I wanted to use this "notificati on when a file closes" to encrypt back the
file and delete it from the temporary folder. This was supposed to run in
a
background thread, some kind of "garbage collector".

I am not sure if the solution You suggested can guarantee the
"transactio nal" sequence of operations. If I get the exclusive access to a
file in one moment (so in that moment nobody else is using the file),
release
the file and try to delete it in the next moment, what is the guarantee
that
somebody else will not try to open this document in the meantime.

For example, the user started opening a file, and this operation lasts for
some time. The file is still not blocked, my "garbage collector" sees that
and deletes the file. In the meantime the slow application decided that it
will finally open the file and it does not find it. What do You think
about
such scenarios. Are they possible? Do I have to worry about such things?

The solution of deleting all files at application shutdown is also not
good
enough, because the files are "secret", and it would be much better to
secure
them immediately after the user finishes using them.

Ana.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Ana,

The only way I can think of to do this would be to have a thread that
would constantly try and open the file without sharing it, and by not
allowing other processes to read it. You can do this by calling the
CreateFileEx API directly and checking the return value, or by trying to
create a FileStream which does the same thing and catch the exception
that
is thrown when it can't be created. Then, you can fire an event from
this
thread that constantly polls to try and do this.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ana" <An*@discussion s.microsoft.com > wrote in message
news:E3******** *************** ***********@mic rosoft.com...
> Hi!
> I have problems with the following scenario:
> My application is developed using C# under .NET. It must run on all
> Windows
> versions starting from Windows 98.
> The user must open different documents (txt, MS Office files, pdf,
> pictures,.) from inside my app. It must start the file with the
> adequate
> external program (Notepad, MS Office programs, Acrobat Reader, some
> Picture
> viewer,... ) and be notified when this programs closes the document.
> I have tried using various algorithms described on the newsgroups, like
> using WaitForSingleOb ject and "Shell and Wait", but the problem is that
> it
> does not work when the program being called is already started.
> For example, when Outlook is started, the winword.exe process is
> running
> by
> default. Opening a MS Word document from inside my app does not start a
> new
> winword.exe process. Instead it just opens my document in a new Word
> window.
> Because of that, starting winword.exe from my app, does not actually
> start
> it, and I can't therefore wait for the process to end or check exit
> code
> or
> do something similar.
> I also tried enumerating the running processes, using
> System.Manageme nt..ManagementC lass methods, but I can't get the
> information
> about the files that a program has opened, just about it's related DLLs
> and
> EXEs (process.GetRel ated("CIM_DataF ile")).
>
> I would like either to be notified when the file is closed or check, by
> using some kind of a file handle, if my file is still opened.
> Does anybody have a suggestion how to do it?
>
> Thanx, ANA.


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1388
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office...
6
3203
by: TIM | last post by:
for example i have one simple programm int main() { int test = NULL; while(1){ printf("%d\n",test); getch(); test++; }
6
5277
by: Kiran | last post by:
Hi, I have program, which opens file at the startup and logs error messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not report any error while writing to the open file handle. On Windows, file shows-up again in explorer and automatically deleted finally when program...
0
1067
by: timbobd | last post by:
Using Visual Basic .NET 2003, I have created a combobox with custom code for "autocomplete" functionality and to trigger a DropDown event when the user starts typing. I want to do a database lookup after the user has selected a new value. However, since this can query hundreds of rows, I don't want to do a lookup every time the user types a...
2
1942
by: LordChaos | last post by:
Dear all, I am a newbie in Java, I got the following problem: I am going through a list of directories. The programm looks inside each directory for specific files and writes them in an File-array. Then it goes through this array and executes the external programm "naccess". naccess takes a file as input and produces 4 output files....
4
7904
by: nrocha | last post by:
Hello, I've been trying to detect when a user closes a page or navigates away from it. This is part of a solution for locking pages (that is, if a user enters a page, no one else can enter it while he doesn't leave). I'm using the window.onunload event to trigger the unlocking of a page, but I don't want the page to be unlocked when a...
0
1077
by: john ciriello | last post by:
Hi, I am trying to detect when the help window that pops up for a control can be detected when it is closing. Right now when you click anywhere the window closes but there does not seem to be anyway to detect it. Thanks.
36
5354
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather...
0
7661
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7978
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8028
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2164
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1263
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
987
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.