How do I detect that Excel is using a file?

Asked 2 years ago, Updated 2 years ago, 98 views

I would like to detect that Excel has finished editing the file, but I would also misdetect the exit of the Excel process and the way I poll for open file read rights.
Is there a way to make sure you've finished editing?

For now, it's just my imagination, but
https://support.microsoft.com/en-us/kb/981094/ja
I feel that excel will release the xlsx file once when it is overwritten and saved.

As for the code, we are monitoring the termination as follows.

System.Diagnostics.Processp=System.Diagnostics.Process.Start(filename);
if(p!=null)
{
   p. WaitForExit();
   while(System.IO.File.Exists(filename))
   {
       System.Threading.Threading.Sleep(1000);
       try
       {
           using(System.IO.FileStreamfs=
               US>new System.IO.FileStream(
                   filename,
                   System.IO.FileMode.Open,
                   System.IO.FileAccess.Read,
                   System.IO.FileShare.None))
           {
               break;
           }
       }
       catch
       {
           continue;
       }
   }
}
else
{
   while(System.IO.File.Exists(filename))
   {
       System.Threading.Threading.Sleep(1000);
       try
       {
           using(System.IO.FileStreamfs=
               US>new System.IO.FileStream(
                   filename,
                   System.IO.FileMode.Open,
                   System.IO.FileAccess.Read,
                   System.IO.FileShare.None))
           {
               break;
           }
       }
       catch
       {
           continue;
       }
   }
}

Thank you for your cooperation.

c# windows excel

2022-09-30 20:47

3 Answers

Excel file operations from a file system perspective are very complex and tricky.
To be more specific, simply "save as" opens, closes, and does the following:

Book 3.xlsx Created/
Book 3.xlsx Deleted/
~$Book3.xlsx Created/Hidden, Archive, NotContentIndexed
~$Book3.xlsxChanged/Hidden, Archive, NotContentIndexed
A4AC1610 Created/Archive
A4AC1610 Changed/Archive
A4AC1610 Changed/Archive
A4AC1610 Changed/Archive
A4AC1610 Changed/
A4AC1610 renamed to Book 3.xlsx

This seems to be the process of writing without errors under complex conditions, such as checking access rights and writing to shared folders on the network.
The problem with your question is that Excel is likely to create a temporary file that is different from the real file, delete the real file, and replace it.

Therefore, as far as the sample code shows, it seems that Excel instance creation is done from the app, so in this case, I think it would be better to start Excel automatically instead of monitoring it from the file system.
How about creating an Excel instance with an Excel.Application() object and receiving an Excel event to handle WorkbookBeforeClose events, as shown in the sample below?

HOW TO: Handle Events for Excel by Using Visual C#.NET
https://support.microsoft.com/en-us/kb/823981
Methods: Visual C# is a great way to handle events.NET (machine translation)
https://support.microsoft.com/ja-jp/kb/823981


2022-09-30 20:47

The language has not been identified.
Since EXCEL is the only tag, I'll talk about it on the premise of EXCEL-VBA.

How do you determine .ReadOnly by checking the "read rights to open files"?

In the Microsoft Support Sample Code,
Open filename For Input Lock Read as is actually locked and checked.
https://support2.microsoft.com/default.aspx?scid=kb%3Bja%3B291295

Have you tried this?
I don't know if it's VBA or not.If it's not a VBA, please write down the language as well.


2022-09-30 20:47

After you add a little bit of processing to get the read-exclusive lock,
Why don't you try to retrieve it again in a second?

If you misjudge a document file as finished editing, you can avoid it.


2022-09-30 20:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.