My program will have to be run on different computers at the same time in the future. How can I open the file when my app loads up, then deny access to anything else EXCEPT the current instance of my app? (first come first serve for access to the file).
Or a way to just flag the file in a particular way and I can run a check on Form Load to see if it is flagged or not and pseudo-lock it by just disabling any button that triggers interactions with the file.
This is just to prevent more than one person from working with the same file at once.
Basically any method to allow this will be fine. I just need a starting point and I can tweak and optimize as I go on.
You can open the file with the File.Open and pass in FileShare.None as your sharing type. This will restrict others from opening the file.
File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)
Edit:
Just guessing from some of your other questions that you are calling XElement.Load on this file later in your application. One of the overload for Load takes a Stream. The File.Open method returns you a FileStream. You could just open the file immediately on application open, and then when you are ready to read the XML file, just pass the FileStream into XElement.Load.
Related
I am createing a gif file from jpg files , using a generic snipit that use MemoryStream, BinaryReader and BinaryWriter.
The gif is created fine, but when i go to delete the jpg files I get a file in use exception.
I know its inside this process(subroutine) since there are no other calls to these jpg files. Also I close and dispose of the above process(MemoryStream, BinaryReader and BinaryWriter).
After continuing the program for a few seconds I am able to go in to the folder and manual delete, but I cant figure out what is holding them open.
Which makes me think that this is the line:
Image.FromFile(Files[I]).Save(MS, ImageFormat.Gif);
but how do i wait for it to finish so i can carry on with deletion programmatically?
Or is the problem maybe elsewhere?
I have a program that allows users to index files in a database and then said files are moved to a fileserver.
Current when they import a pdf it is displayed in a WebBrowser control, with WebBrowser.Navigate, so that they can see the file and then select the required indexing information from drops down. When done the file needs to be moved and the original deleted.
However I am running in to a problem where viewing the file through the WebBrowser locks the file and it cannot be moved. I have tried getting the WebBrowser to point at another file but this doesn't seem to release the file from being accessed.
Is there a way, short of disposing of the WebBrowser at run time to force the program to release its lock on this file?
I'm using the following the code to open a file that resides elsewhere on the computer:
System.Diagnostics.Process.Start(PathToOpen)
Where PathToOpen is the full address to the file I wish to open. This appears to work as intended for any file type that has an application installed that can open the file.
Whilst this works OK, if the file is on a networked drive, I want my VB.Net code to check if the file is currently in use, if so display a message. Currently what happens is if a second user tries to open the file, it opens read-only (a word file for example) which is handled outside of my application. I want to intercept before the file is opened and stop the process there.
Is this possible at all?
You are basically asking if it's possible to monitor files on a system that doesn't even belong to you ?
Word does know about a file already in use because it creates a hidden file next to the one you open. if there is already a hidden file, it means the file is already in use.
Other applications use different ways of knowing if a file they can use is already opened somewhere else.
In order to do what you want to do, you need to know how all the applications handle this problem...
A possible solution would be for you to create a small hidden file next to the file the user wants to open (just like MS Word does). Only problem, you need to destroy the file when it's closed by the user, and you have no way of knowing that...
I have a file called PolicyLookup.sql that sits in my application's root folder. My app loads this file into a text box, so that users can edit it and overwrite the original file by pressing a save button. This all worked perfectly during test, however after deployment users are unable to save the file due to write issues within C:\Program Files.
Is there a way around this - or is there a better way to implement this type of thing? One solution that springs to mind for me is placing the contents of the PolicyLookup.sql file within a User Setting - however it intrinsically feels wrong to me to put the entire contents of a file within a settings variable.
Ordinary users do not have write permissions on %ProgramFiles%. If you need to save a configuration file then put it in a subfolder of %APPDATA% (which for me is C:\Users\Gord\AppData\Roaming) or some other place where a regular user is allowed to write.
Good evening,
I have the following problem to solve:
I want to add to an Excel file the contents of a bunch of user-generated .txt files. These files are generated throughout the day and sent over FTP to a folder, which is being constantly monitored by the program to see if there are new additions.
If the program finds new .txt files in it, it opens the Excel file which is to be edited, adds the info and then closes the Excel, saving the changes.
At the same time, users have to open these Excel files to check the new updated info and deal with it accordingly.
The program's execution is somewhat like this:
Infinite loop checking if the folder is empty or contains new .txt files.
If the folder is not empty (hence there is info to be added), it checks whether the Excel file is open or not.
If the Excel file is closed:
Opens it programmatically and adds the info to it.
Saves the Excel file and quits.
Backups the .txt to another folder in case there was some sort of error.
If the Excel file is open:
Keep checking until it is closed.
To check if the folder is empty or not I use: System.IO.Directory.EnumerateFileSystemEntries(path).Any()
To check if the Excel file is open I use:
System.IO.FileInfo(path) and a FileStream which, inside a Try-Catch clause, opens the FileInfo in the following mode: info.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
For the opening, editing and saving/closing of the Excel file I use Microsoft.Office.Interop.Excel.Application, [..].Workbook and [..].Worksheet.
The main problem comes when a user opens (through the normal Office Application) the Excel file in the middle of the addition process. I was hoping to find some sort of lockage of said file so the user cannot interrupt the editing process.
Any ideas on how to combine both types of opening/editing the Excel file?
Thank you so much in advanced.
TL;DR: How can I prevent, programmatically, a user from opening an Excel file while a program is editing that very Excel? Or at least, open another instance of it so the process is not interrupted?
PS: If any further code should be needed, I'll gladly post it :)
I think you need a better solution. It sounds like you are maxing out the capabilities of Excel. You would be better off, if possible, to use a database.