Directory.GetFiles() causes blue screen of death - c#

I was searching for a test project I had coded a few months ago, and could not remember in which solution I had placed it. I decided to write some quick code to get all my Visual Studio 2010 projects' c-sharp source files, and look through them for a literal I know should exist in the file.
I started in debug mode, and when my code executed this statement, I got a blue screen of death:
string[] results = System.IO.Directory.GetFiles(#"C:\Users\<user name>\Documents\Visual Studio 2010\Projects\", "*.cs", SearchOption.AllDirectories);
After a reboot, I checked the event logs, and I only saw the generic unexpected shut-down messages. I brought up the temp project, ran in debug mode again, and got the BSOD again. If I remove the SearchOption.AllDirectories param, the code doesn't break, but it doesn't get me the results I want either. But when I put it back, BSOD again.
Does anyone have any insight as to what could be causing this, or where I might be able to look (logs, etc) to see what the issue is? I did not see a kernel dump in the Windows directory, and the event log doesn't show anything that would stand out to me. FYI, I can run the same search from Windows Explorer with no errors and all the files returned.
* Update *
I ran the same snippet of code but changed the file mask to *.txt, and it runs with no issue, so the .cs extension is playing into the error for some reason. Could it be having an issue listing the .cs file that I am running in debug?

I guess there is a disk hardware error in one of your projects. Try enumerating all files there recursively and log it to see which one causes BSOD.

Related

How to stop visual studio from scanning all files and folders in my website when degugging

While trying to debug an asp.net website in visual studio 2013 professional I am having a weird issue. The website is simple but uses flat files and folder structure and files in it for performing some operations. There are two important folder in this website that contains 100 thousands of files and images. When I try to start debugging and do not remove files from these two folder then VS crashes and restart, but if I remove files then VS proceeds normally. The files in these folders are csv, txt and image files. What I can understand is that VS goes through all these folders and files before starting the debugger, hence somehow I need to tell VS to not to consider these folders. I am not able to find anything related to it. Please help. Thanks in advance.
Perhaps take a look at your Event Viewer first and see if you can find anything regarding VS crash.
Also you can try running Visual Studio from the command line devenv.exe /log and then load your solution. If crash still happens, then go take a look at following file %APPDATA%\Microsoft\VisualStudio\<version>\ActivityLog.xml. Perhaps you would find some more details about the crash.
If you happen to be using ReSharper, you might want to disable it and see if that would make any difference. ReSharper likes to index source files in the background which could lead to some usability issue in VS.
Answering for anybody who faces similar problem.
For some reason I dont know why, when starting degugging visual studio was scanning each and every folder, even the folders which doesnt contained any code file, this was making VS very slow due to very high number of files and out of memory exception was being thrown. Since in my case these folders doesnt contained any code file, but still were needed to run the application, I made them hidden from window explorer. This solved the problem as now vs ignores them, while still the program can output report and read reports from these hidden folders.

Locking files when building in Visual Studio 2010

Hello there, Stackoverflow.
Recently, when I've been programming in Visual Studio 2010, I've been getting the problem with VS locking the bin/Debug/(ProjectName).exe file when trying to build and gives me the error below after trying to build the project 10 times:
Unable to copy file "obj\x86\Debug\TileEngine.exe" to "bin\x86\Debug\TileEngine.exe". The process cannot access the file 'bin\x86\Debug\TileEngine.exe' becuase it is being used by another process.
The problem appears when I edit the source and then try to Debug.
I've checked using different programs, and the only program using the file is Visual Studio.
If I wait for about 10 minutes before trying to build, it seems to work properly, but when trying different things, it isn't good needing to wait 10 minutes before trying something.
I've tried different solutions both on this site as well as everywhere I can find on Google.
Some solutions I've found, but haven't worked for me
Solution 1 - Using a pre-build script
In some different questions here on Stackoverflow, I've found one solution being that you go into Project Properties > Build Events and then in the Pre-build event command line add:
if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
This made it possible for me to build the project one more time than I usually could, but when editing the code again, and then building, the same error appeared.
Note: Trying to build a release instead of a debug build seems to break the pre-build script and it exits with the code '1', which seems to make VS unable to build properly. Removing the pre-build script makes it work like "normal" again, still with the same error though.
Solution 2 - Running Visual Studio as Administrator
This is another solution I've found, but havent worked either for me, so I assume that Visual Studio already have all the permissions required and running as Administrator doesn't actually make any difference.
Solution 3 - Changing the AssemblyVersion
In this question, Visual Studio build fails: unable to copy exe-file from obj\debug to bin\debug, I found another solution that included changing the AssemblyVersion, in the Properties\AssemblyInfo.cs file, to "2.0.0.0".
This, however, haven't made any difference whatsoever for me.
Solution 4 - Closing UserControl designers before building
According to some different answers here and there on the Internet, Visual Studio apparently uses the built project executable to render the UserControl designer(?). In my case, this is probably not it, though, since I use XNA mostly and it doesn't use the UserControl designer.
Solution 5 - Cleaning up resources when application quits
This might be a solution that I have failed to implement properly. I'm just thinking though, that if this is the solution, how come I haven't been required to do it before. I assume XNA unloads everything that gets loaded through the Content pipeline, therefore this solution wouldn't' make any real sense.
If there is anyone that is able to spread some light on this issue, it would be really awesome, as it is stopping me from programming anything really, because I don't like waiting for 10 minutes because I've made a 2 second change all the time.
I've run into this problem a few times myself.
Mine might not be from the same cause as yours, but I'll tell you what went wrong with me and how I fixed it, hopefully it'll be helpful to you.
Basically, my program never fully exited properly, even when it appeared to. It would continue to run, and thus continue to lock down the file.
A quick dirty fix I used initially (and a way to prove if this is the case) is:
Open Task Manager (Ctrl-Alt-Del)
Click Processes tab
Look for your program's name (TileEngine.exe)
Note: There will probably be name_vshost.exe (TileEngine_vshost.exe) That's a VisualStudio thing, ignore that, it's not relevant.
If you find it, it means your program hasn't actual exited fully.
If it's there, click on it and press "End Process"
So if it's there, then for some reason, your program didn't shut down, like mine did.
Often, this is from a thread being launched and forgotten, or an Async task that never completes, or something like that.
Make sure in your OnExiting(..) void function that you kill all running threads.
If your program is still running despite best attempts to close all threads and other blockers, you can use the very dirty bad method:
In OnExiting(...) run the code "System.Diagnostics.Process.GetCurrentProcess().Kill();" - this will taskmanager-style forceshutdown the current process... this is only as an emergency I-can't-make-it-work-any-other-way method.
I think I found the solution myself.
In the Project Properties, "Enable the Visual Studio hosting process" wasn't checked. Checking it seems to have fixed the problems, at least for now.
Got reminded of it from mcmonkey4eva's post. So thanks for that =)
And thanks for the other replied I've got. Stackoverflow is awesome!
Have you checked if any files are being blocked by your firewall? When I switched to the full version of Avast I find I have to disable the File System Shield It loves to remove my executable files when I try to run my visual studio projects.
I had problems when upgrading to VS2012 Professional. (SDK, .Net, Visual C++ Redistributable package)
ENSURE ALL OF THESE ARE COMPATIBLE WITH THE CURRENT VERSION OF VS YOU ARE USING
What I did, was I ended up uninstalling EVERYTHING that was associated with both Visual Studio downloads. If you are able to remove and save your project files elsewhere and then bring them back. Go through all your program files to see if there is anything hidden in the wrong folder and check your C drive.
Which meant downloading and reinstalling (fresh):
I think if you clean out your program files, it should be ok. I wouldn't recommend going into your register unless you are very sure of what you are doing. IF you have already made changes to the register then we'll have a look at that and other options (if this doesn't solve your problem).
Try removing the readonly check from your solution by unchecking at the folder level.
I ran into this problem and in my case was due to having bin included in the solution; as soon as I excluded the bin folder from my solution the problem went away.
Nothing helped, not prebuild commands, neither designers closing, but I figured out a way that helped me, just changing from debug to release and vice-versa releases locked files and you can delete them without closing the IDE.
I regularly get this problem if I switch from Debug to Release and then immediately F5 to compile. Crazy as it sounds, waiting for, say, one minute after switching between modes will prevent this.
If it's locked, the only solution is to close Visual Studio and re-open.
I solved this problem organizing my resources on solution. I noticed this error when I put some images on my application at the same solution folder.
So,
I'd removed all images and resources from app, save without it.
Moved the images to outside Solution folder.
Open the solution and add this images again, using "Import" button at controls.
If you will try this, remember to do with Application Icon, on Project Settings.
Now, everything is working fine for me.
I hope it helps.
You need to disable Windows Indexer as it locks up the file
Follow this Guide how to disable
In my case the problem seem to be caused by the remote debugger. It starts on local machine when you compile with "x64" option. Try to change the project setting (properties/buid) until you reach the final version.
Change Build Platform target from x86 to Any CPU.

Can't rebuild my application anymore

I'll give you 2 versions of my problem to describe it, first the short version.
When I try to build my application it does that perfectly and my app works but when I try to rebuild it, that fails and gives an error message saying that 2 files are missing (bin/debug/MusicPlayer.exe and bin/debug/MusicPlayer.pdb). When I then try to build normally, it fails to with the same error.
Now the longer version:
The way this happened was quite out of nowhere, I program on 2 different locations (both in Visual Studio 2010 sp1) but to always have the recent version in the right place I copy the entire solution folder to a usb stick, this folder I copy over again on the other PC and use it to continue were I left off, I've done this many times without problem. Yesterday however, I got an exception while testing the app. But instead of showing this in my code, it was complaining that it could not find the program.cs file (it was there, but apparently it had a different checksum (md5) and it asked me to use this one. After a while I found what part of my code caused the exception and fixed it. Since then I haven't got that error anymore. But when I later tried to rebuild instead of build, it gave me the error described in the short version. I have tried to fix it, but apparently I was not very successful.
Basically, what I think it does is delete the files in the debug folder that need rebuilding and then gives me the error of missing files (the files that it deleted) and thus failing to rebuild succesfully because these deleted files the normal build option won't work to.
(What I then do is re-copy them from my usb to make the normal build work).
Don't know if it's important, but I program in C# and I'm still learning.
Also when I copied it to my usb I believe I had no errors and the app was working fine (except the part I fixed later, which I could not test at that location). And when I build the entire solution it gives some warning ==> "Assembly 'bun/debug/MusicPlayer.exe' is incorrectly specified as file" and this for 5 files.
Does anyone know how to fix this rebuild problem and if necessary the warnings?
Thanks in advance
(and sorry for my bad English)
OK it seems like you might be confusing yourself with your directory structure here. If you want to add Content or Resources (Images, Text Files, etc.) you should place them in a folder within your project (not called bin or obj). All your build files will go here. Instead place the Content in another folder and Right Click -> Properties and Set the build action to "Content" or "Resource" and set the Copy to Output Directory (bin folder) to "Do not Copy" or "Copy if newer."
The project is failing because you have the built executable "MusicPlayer.exe" in your project. So Visual Studio is trying to build an executable file in adding to the project. Restructure your directories or remove "MusicPlayer.exe" ever time your build your project.
Do you have any anti-virus software running? They can go way too aggressive on removable drives. You are actually continuously deleting and creating a runnable program on usb stick by recompiling.
Just a guess tho...
Sounds to me like you have a pre- or post-build event with a hardcoded path in it. Fix that using variables and it should be ok.

I need help with this error please: Could not write to output file...file is being used by another process.

Could not write to output file 'C:\Users...\Documents\Visual Studio 2010\Projects\Application 2\Application 2\obj\x86\Debug\Application 2.exe' -- 'The process cannot access the file because it is being used by another process.'
I have been getting this error since last weekend. I have searched it on the web and found a few solutions which I have tried to no avail.
I have closed all my design views and then tried running.
I have closed ALL windows and then tried running.
I have unloaded the project and reloaded it.
I have deleted the 'obj'-folder in my 'application 2'-folder.
I have closed Visual Studio and reopened my project via the start page as well as via the solution exe.
I have edited the pre-build event command line with something that supposedly unlocks the file and then tries building it.
I have downloaded 'ProcessExplorer' to try and identify a process that might be locking this file and ending it.
-I have even done a total system restore to 3 weeks ago in-case there was an issue.
Some of these solutions worked the first time I did it, but thereafter it doesn't.
The only thing that seems to be working consistently is if I restart my computer. It then builds one to four times successfully, but then starts giving me this error.
I haven't done anything drastically different in my code that might affect my project in such a way to suddenly give me this error. I am using C# and doing a winforms application connecting to a postgresql database.
Just a note: It did this before, but then I just click NO to run previous build and rerun it, then it works. It now is a permanent problem.
I hope someone would be able to help me with a fix for this.
Thanks.
My guess is that your virus scanner is getting in the way. Try to disable real time scanning and if that helps.
I've encountered similar problems when using third-party addons that are supposed to analyze code and things like that.
There must be something attaching to your program that does not get properly closed/disposed/etc. when the program ends.
I would look around and see if you can find anything that may be using your executable. Then you can try to turn this off, or use the task manager to kill the process.
In the "xxxx.csproj" file,search the file name 'Application 2.exe'
If you see the following XML
<Reference Include="Application 2">
<HintPath>obj\Debug\Application 2.exe</HintPath>
</Reference>
remove!!~~~

visual studio C# locking .exe file

I am working on a gui app written in C# using visual studio and I am running into a really annoying bug where when I make changes to the application, it debugs correctly the first time I try to debug it, but when I try to debug the application again I get an error say that it can't copy an exe file from the obj directory to the bin directory.
Restarting visual studio fixes this.
It's really annoying to have to restart all the time though. There seem to be a number of posts online about this, but no one seems to have found the solution. Has anyone here been able to resolve this?
try VSCommands 2010 extension.
When you get the error again righ click on the message in error list and click 'Apply Fix' from context menu. This will tell you which process sits on the file and allow you to terminate it.
In a longer run, it may be that you have a background thread running which doesn't terminate when application finishes.
I have the same problem. I found in another thread (can't remember which) that for some unfathomable reason, you can change the assembly version declared in your AssemblyInfo.cs files from
AssemblyVersion("1.0.*")
into
AssemblyVersion("1.0.0.0")
Or some other explicit version, and the problem goes away. This worked fine for me. However, I would like a solution that would allow me to use the wildcard version as well. Or an explanation of why the assemblyversion causes the file to be locked.
is this a service? you may have to installutil -u prior to recompiling
In my case it was the Anti Virus software Avira antivir. When I added the publishing folders in the exceptions the problem went away.
Avira usually takes a long time to scan stuff in the background, apparently locking exe files before they can be overwritten. After the scan the file is unlocked. When my develop/test cycle is less than 30 seconds (which it often is) it will trigger this error after retrying 10x.

Categories