VB.NET: Detect if a text file is open - vb.net

Is there any way to determine if a text file is currently open in a text editor? Or better yet, is there a way to trigger an event when a text file is opened (from any program)?

Using the FileSystemWatcher component you can detect Changed, Created, Deleted, and Renamed events statically.
If you want to detect Last Access you need to manually set the NotifyFilter to include LastAccess.

When most editors have a file open they typically follow a set strategy:
1. Open File
2.Read entire contents into buffer
3.Close File
Then your program runs. Since the file is already closed, any attempts to open it will of course succeed. Using a FileSystemWatcher this can be detected if a file is open or closed. However, you will not be able to detect if the file has been open prior to your program running.

I think I'll make a Timer that checks the last modified time of the file.... and when the program starts, it'll get the last modified time.

Related

How do I open a file with a specific application rather than its default application?

I am trying to open files using a specified executable; just like as if you were to right mouse click on a file then scroll to "Open with"
I tried what kaymaf said and reviewed the docs, but I cannot seem to get this to work.
Dim FI As New FileInfo(GetFileNameFromListViewItem(ListViewCollection.SelectedItems(0)))
Dim GetExif As Process = System.Diagnostics.Process.Start("C:\Users\*username*\Downloads\exiftool.exe", FI.FullName)
This just ends up open the executable and rather than opening the file with the executable.
You would like to open a file with your program using the Windows context menu; and do you want to get an entry in that menu? If that is not correct, the answer can be deleted.
I found this in a German forum, and they refer to this site:
This is the translated text:
One possibility would be that you register your file extension and your program in the system to open this file extension. As soon as the system knows everything, you only need to right-click on the file(s) and in the context menu, in addition to the standard entries, another menu item for opening these files is displayed. If you select this menu item, your program will start automatically if it has not yet started, and you can read out / determine the path to this file or several files in your program and process it accordingly. How it all works is described here: ookii.org/Blog/opening_files_via_idroptarget_in_net
On this page there is also a sample for download (start text files with your own program via an additional entry in the context menu / display paths to the files). It is not a VB, but it should be translatable without any problems. Corresponding information on the page and the comments should be observed.

VS code new update doesn't retain opened file (tabs) when switching/opening another file

Early February VS Code update seemed to have messed my entire IDE settings.
Version: 1.42.0
When I open a file and switch or drag another file to the main working area the previous one just closes even if I double click on the tab to retain the opened file
The only property which could potentially help is workbench.editor.enablePreview
What do I do to revert that so that I can have multiple tabs/files open again?
Modify the following property in order to retain opened files
workbench.editor.showTabs
Check the checkbox

VB.Net remove lock attribute from files in directory

I am making a little program and I need to be able to remove the Lock attribute from all files (no matter the extension of those files) in the specified directory. I haven't find any way to do so on the web so far, so I really have no idea how to start a basic code for that job, so all I can really do is give you the elements of my Windows Form:
txtDirectory: This is where the directory is being
displayed/selected. All files in the displayed directory needs to be
getting the Lock attribute from them - see btnConfirm to know more.
btnSelectDirectory: This button opens the directory seletion
dialog. This section does not need any code - but feel free to add
code if you think it could improve my program.
btnConfirm: This
button is where, when clicked, all files in the specified directory
will be removed from the Lock attribute. This is what I really
need help with.
How can I do so?

Exception trying to Start a process after copying the file

I'm getting an annoying error like:
The process cannot access the file 'C:\Program Files (x86)\AceHc\trfpt.exe' because it is being used by another process.
The error happens when I try to use Process.Start after File.Copy the same file.
Code:
File.Copy(PathFrom & "\trfpt.exe", PathTo & "\trfpt.exe", True)
Process.Start(PathTo & "\trfpt.exe")
What am I doing wrong?
Do you have a virus scanner that might be scanning the file directly after the copy and blocking it?
It might be worth trying to temporarily disable it and see if the problem goes away.
Also, depending in the size of the file, maybe the EXE file is still being copied (that is, the copy function doesn't block que program flow, your code continue and the copy process continue in background).
Download Handle from Windows Sysinternals and run it as admin from cmd to get a list of processes which hold a handle on that file:
handle.exe trfpt.exe
A wild guess - do you have a Windows Explorer window open looking at the folder 'Pathto' ?

Updating distributed Word VBA macros - is it possible, how would you do it?

I have a VBA macro which runs in Word 2003. It is one module and the code attached to a userform, and it goes to about 30 client sites. When I first rolled this out I sent someone round to each site to place a .dot template in the Word Startup folder of each of 30 boxes, which then makes the macro appear as a button on the toolbar for each user.
All the users are internet connected.
The location of the .dot template varies from machine to machine, so "installers" would be out of play I presume?
I am anticipating that the macro made need to change from time to time. Is there any way I could get the user to press a button to get the macro to update itself?
Whether its push button or auto run checking for update, I would presumably need to know how to determine where the macro is running from (path) without a document necessarily having been saved, and how to find path to the Startup folder tools>options>file locations but programatically .
This can be done.
create 2 .dot files, loader.dot (checks for updates) and worker.dot (the main macro you are using and want to be able to update automatically).
the first loader.dot should go into C:\Program Files\Microsoft Office\OFFICE11\STARTUP
This is a trusted location and the macro will be loaded into any Word document. Use an AutoExec() subroutine to ensure it runs whenever a word doc is loaded.
worker.dot can go in any fixed installation location e.g. C:\yourapp\worker.dot
The logic in loader.dot is remove any reference to worker.dot (I had to enable trust of VBA model in macro security settings) check for an update using an http call, if new version available download it overwriting worker.dot, then add reference from file specifying worker.dot
I'm not sure that the user-presses-a-button model is ideal.
I'd look into building a "loader" template. Its job would be to run at startup and look for updates to the template/macro you already have and download/install/whatever such files when they change. Then load whatever's installed locally for use.
You should also think about whether it's a problem if an update is available but someone doesn't use it (perhaps they started Word before the change was uploaded, or they're offline). Also, do you need to know what version people are using? Have your loader report back, via HTTP, email or some other method (Twitter?)
Any such loader needs to either very stable (otherwise you just replace chasing around replacing the target template with chasing around replacing the loader) or able to update itself when necessary.