filelock

How do I get WPF's DocumentViewer to release its file lock on the source XPS Document?

我是研究僧i 提交于 2019-12-03 12:40:49
After showing an XPS file in the WPF DocumentViewer, and closing the DocumentViewer instance, the XPS file is locked and I cannot delete it. I need to release the lock on the XPS file so I can delete it, write another one with the same name, and optionally display that new XPS file in a new DocumentViewer instance. I need to do this in the same app instance - without having to close the app (this is a Print Preview scenario). In other words, how would I get the following code to run without throwing an exception at the "File.Delete(tempXpsFile);" statement? var tempXpsFile = @"c:\path\to

How can I simulate a “locked” file (one which has a write lock)

落爺英雄遲暮 提交于 2019-12-03 01:46:41
I am trying to debug a problem where users occasionally have locked files which they try to open. The code appears to have correct exception handling but users are still reporting seeing error messages. How can I simulate a locked file so that I can debug this myself? EDIT: For Windows. depends, but in case, MS word locks if you are wonderig if your application lock files and it do not relase locks: just modify a bit your aplication (to create a testapp) and never close the file (and keep it runnig) try this: ( >&2 pause ) >> yourfile.txt >> opens yourfile.txt in append mode see this for a

Concurrently write to XML file

荒凉一梦 提交于 2019-12-02 01:34:30
I have multiple processes running on different machines which are required to read/write to a shared XML file, for this I am using DOM with Java and FileLocks (While I know that a database would be a more effective approach, this is not viable due to project constraints) . To make changes to the XML file, the relevant process first creates an exclusively locked channel which is used to read the file, it then attempts to reuse the same channel to write the new version before closing the channel; this way the lock is never down. The issue however is that I am getting a java.nio.channels

Getting notified when a file lock is released

天大地大妈咪最大 提交于 2019-12-01 21:14:00
[Using C# and Windows as platform] I have a camera that writes JPG files to a local folder in my PC. I want to load each file the camera drops, so I have a FileSystemWatcher that notifies me whenever a new picture is created, but the camera locks the file while it's being written , so if I try to load it just after being notified of its creation, I get an exception saying that the file is locked . Currently, I have a while loop (with a Thread.Sleep) that retries to load the image every 0.2 seconds, but it feels a bit dirty. Is there a more elegant way to wait until the lock has been released,

How to prevent file from being overridden when reading and processing it with Java?

筅森魡賤 提交于 2019-11-30 22:57:48
I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it? That is, some way to make it read-only, keep it "open" or something... This would be done in Windows environment. br, Touko you want a FileLock : FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel(); // Try acquiring the lock without blocking. This method returns // null or throws an exception if the file is already locked. FileLock lock = channel.tryLock(); //

How to prevent file from being overridden when reading and processing it with Java?

北战南征 提交于 2019-11-30 18:18:57
问题 I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it? That is, some way to make it read-only, keep it "open" or something... This would be done in Windows environment. br, Touko 回答1: you want a FileLock: FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel(); // Try acquiring the lock without blocking. This method

Using FileChannel to write any InputStream?

不问归期 提交于 2019-11-30 12:06:56
Can I write any InputStream into a FileChannel? I'm using java.nio.channels.FileChannel to open a file and lock it, then writing a InputStream to the output file. The InputStream may be opened by another file, URL, socket, or anything. I've write the following codes: FileOutputStream outputStream = new FileOutputStream(outputFile); FileChannel outputChannel = outputStream.getChannel(); FileLock lock = outputChannel.lock(); try { outputChannel.transferFrom(???); } finally { lock.release(); outputChannel.close(); outputStream.close(); } However, the first argument of outputChannel.transferFrom(.

Problem with Java file locking mechanism (FileLock etc)

那年仲夏 提交于 2019-11-29 07:53:35
I am creating a simple application for opening and editing xml files. These files are located in a local folder accessed by multiple instances of the application. What I want to do is lock each file that is opened by an instance of the app, so that other instances cannot access it. To achieve this I use the following code: function void readFile(){ File xmlFile = new File("myFile.xml"); RandomAccessFile raf = new RandomAccessFile(xmlFile, "rw"); FileLock fl = raf.getChannel().tryLock(); if(fl==null){ System.out.println("file already locked by another instance"); }else{ setCurrentFile(raf);

How to synchronize file access in a Java servlet?

老子叫甜甜 提交于 2019-11-29 04:49:25
I created a small Java servlet for a simple purpose: Once it is called, it will do the following steps: Read file foo.json from the local filesystem Process the data from the file and do some changes to it Write back the changes to the file Simplified version of the code: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileInputStream inputStream = new FileInputStream("foo.json"); String filecontent = IOUtils.toString(inputStream); inputStream.close(); JSONObject json = new JSONObject(filecontent); doSomeChangesTo

FileStream with locked file

喜欢而已 提交于 2019-11-28 13:18:19
I am wondering if it's possible to get a readonly FileStream to a locked file? I now get an exception when I try to read the locked file. using (FileStream stream = new FileStream("path", FileMode.Open)) Thanks! You should try another constructor. They are documented at MSDN. This one looks like a bet: FileStream Constructor (String, FileMode, FileAccess, FileShare) MSDN Link FileAccess A constant that determines how the file can be accessed by the FileStream object. This gets the CanRead and CanWrite properties of the FileStream object. CanSeek is true if path specifies a disk file. FileShare