How to check if a file is in use




















Virus Total will check the download for viruses and let you know of any threats that were detected. PC users have long been targeted with malicious software online, and the concern is real even to this day. Malicious code is now even making its way onto legitimate download sites, no matter how authentic they have been over the years.

Fact of the matter is that it is no longer easy to be sure what you are downloading is dipped in malware. No such thing as foolproof in the world of security, and this is something you can take to the bank! Despite the best efforts of the security researchers and developers, the online world still remains a dangerous place. This is a program that is downloaded by millions of people across the globe every week.

Back in , a legitimate signed version of the application was found to contain malware. It did not actively harm systems, but did collect information that could be used for nefarious purposes, farming details like the name of the computer, list of installed software and running processes, MAC addresses, and whether the infected machine was running with administrative privileges.

A few days after this news broke, a second payload was discovered, said to be a targeted attack against technology companies. Point being, anything can be infected nowadays — even software that you think you are downloading from previously secure sources. The only way to truly stay on the safe side is to be actively vigilant against files and content that you download from the web.

It is not hard to pick up some clues along the way. For starters, if you are downloading something from a shady source, chances are that your package may come with a virus hidden in the download.

Pirated content, in particular, runs a heavy risk. The website you are downloading it from is another factor. If you are grabbing something from a very basic site, you are probably being duped into downloading malware. Of course, there are always exceptions to the rule.

Most software developers are not web designers. Downloading files is not the only way you can end up with malware on your machine, though. Even loading a website sometimes can expose your computer to malicious code. This is called a drive-by attack, as it happens without you doing anything and simply loading the website.

That said, there are ways to block the virus at its primary stage. There are certain precautions you can take to insure against malware and reasonably ensure that the file you are downloading is safe. At the end of the day, it all comes down to your usage, browsing and download habits.

There are certain, shall we say, best practices before clicking on download links. Listed below are the precautions and preventive measures you should take before clicking the download button. Assess your downloads: Always be aware of what you download on your PC and where you download it from.

Also, if your download source has a forum or user comments section, it is never a bad idea to scan that and read what others are saying.

Check the source: Always download your programs from a trusted source. How to check file is open or not. How to know when a file is locked by another process? File Locked Or In Use. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums.

Type in the name of the locked file or other file of interest in the Search dialog box, then click "Search". Partial names are usually sufficient. A list will be generated. There may be a number of entries. Click one of the entry, it'll "Refreshing handles".

Liked this Tutorial? Probably some more care can be given to the catch block to ensure I'm catching the correct IOException s. I'll probably also clear out the app storage on startup since these files are intended to be temporary anyways. I realize this goes beyond the scope of the OP's question of simply checking if the file is in use but this was indeed the problem I was looking to solve when I arrived here so perhaps it will be useful to someone else.

If you can, it has no lock and you can safely work in the temp dir without getting locks. Else just try to move it again in x seconds. I use this workaround, but i have a timespan between when i check the file locking with IsFileLocked function and when i open the file. In this timespan some other thread can open the file, so i will get IOException.

What do you think? Can i change some thing? Maybe i did not have to use IsFileBeingUsed function at all? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Is there a way to check if a file is in use?

Ask Question. Asked 12 years, 8 months ago. Active 7 months ago. Viewed k times. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to the filesystem and throw an error: "File in use by another process" I would like to find a way around this, but all my Googling has only yielded creating checks by using exception handling.

Improve this question. Selim Yildiz 4, 6 6 gold badges 15 15 silver badges 25 25 bronze badges. Dawsy Dawsy 9, 4 4 gold badges 18 18 silver badges 13 13 bronze badges. All right, you can test it by examining all the open handles on the system. However, since Windows is a multitasking operating system, there is a chance that right after you run the code to determine if the file is open and you deem it is not, a process code start using that file, then by the time you try to use it, you receive an error.

But, there is nothing wrong with checking first; just don't assume it is not in use when you actually need it. But just for this specific issue; I'd recommend not examining the file handles and just try some preset number of times, say before failing. Your philosophy has a bad understanding of exceptions. Most people think exceptions means holy-crap-out-of-doom-something's-wrong-die-die-die. When exception means It means something exceptional occurred that you need to "handle" or account for.

Maybe you want to keep retrying for data access, maybe the user needs to know that you can't get a connection.

What do you do? You handle the ConnectionFailedException and notify the user, so maybe, they'll stop trying after an hour, and notice the cable is unplugged. Lee Louviere the op has a valid dislike for working with exceptions. If you can easily use filexists method to know if a file exists what similar command exists to know if the file you want to work with is in use?

In fact i believe that is the question the op is really asking. Using File. Exists is good, but like the answer says, it's not always possible to avoid them. For example, a file could exist between the time you check if it exists and when you actually open it. That indefinite period is good enough most of the time, but there are no guarantees and so an exception handler should wrap the call to open it, etc. Show 5 more comments. Active Oldest Votes.

Open FileMode. Open, FileAccess. Read, FileShare. Improve this answer. Collin Dauphinee ChrisW ChrisW 8, 1 1 gold badge 18 18 silver badges 33 33 bronze badges. This is a great solution, but I have one comment - you may wan't to open the File with access mode FileAccess.

Read since ReadWrite will always fail if the file happens to be read-only. I think this is a great answer. ChrisW: you might be wondering what is going on. Do not be alarmed. You're just being subject to the wrath of the Daily WTF community: thedailywtf. ChrisW Why is that a bad thing. This community is here to point out good and bad answers. If a bunch of professionals notice this is a bad thing, and join to downvote, then the site is WAI.



0コメント

  • 1000 / 1000