When I try to build a project twice in successon, i get the following error
Error 2 Unable to copy file "obj\x86\Release\iFileUploader.exe"
to "bin\Release\iFileUploader.exe". The process cannot access the
file 'bin\Release\iFileUploader.exe' because it is being used by another process.
If I close Visual Studio and reopen it, I can compile it again but only once.
I have my projects hosted on a Windows network share. The server runs Windows 2008 R2 and Im on a Windows 7 machine and Ive tried setting everyone full control on the share and the folder permission, to no avail.
Ive even run the Unlocker program and checked the Windows Share & Storage Manager to see if anything is using it and nothing is! I cant delete the file when this happens either until I close VS.
Is there a setting I am missing in Visual Studio?!
UPDATE
Have removed all antivirus/antispam, disabled firewall.. so zero security.
Have disabled "Enable the Visual Studio hosting process"
Visual Studio is some how the colprupt with not releasing the handle
UPDATE
Another thread that has exactly the same problem but from years ago !!
Destroy process-less console windows left by Visual Studio debug sessions
UPDATE
I copied the files locally, and that didnt work. So I created a new project and then copied all the code in to the new project and now its working (with the files stored locally)
check your antivirus program is using it or not.
Alternatively use Process Explorer and find for the string - "iFileUploader.exe" and see who's using it. You can easily get the handle and close it.
Several points:
Check if you don't have a process iFileUploader.exe already running in the Windows Task Manager.
Check that nobody is not running a project that could use iFileUploader.exe.
Christian even if you are have your Stream in a using(){} do not declare a new variable of StreamReader for example do not do this
using(StreamReader streamReade = new StreamReader)
{
..... // if you do you will run into that stream being locked
}
//Declare the StreamReader variable out side of the using and then within the using do something like this.
StreamReader streamReader = null
using(streamReader = new StreamReader()
{
}
Make sure that where you have created an instance of the StreamReader or StreamWriter that you have closed the stream.
For example I would create at the application level a StreamReader / StreamWriter instance and set it to null
Class SomeClass
{
public StreamWriter streamwrtFileToCreate = null;
public FileStream fstreamFileStream = null
public SomeMethod(string FileName, string FilePath)
{
FileStream fstreamFileStream = new FileStream(#FileName + #FilePath, FileMode.Create);
streamwrtFileToCreate = new StreamWriter(fstreamUpdateRpt);
streamwrtUpdateRpt.AutoFlush = true;
}
}
Also it would help if you post the exact example of code that you are using when you create the FileStream Instance
I had this issue in VS 2012 windows 7.
Root cause is Avast Antivirus.
Fix is disable Avast whenever needed to work on C++ console
applications
(or)
->Go to Avast Settings
->Active Protection
->File System Shield (Customize)
->uncheck Scan programs when executing
Related
I'm new in xamarin forms and I need to open/save some data. I chose interlanl storage, becosue it won't be a big database. Everything works fine I can save and then read fiel. The problem is showing when I'm trying to move the app from the computer to my smartphone. First, I don't understand why in the folder app/Android/bin/Debug (on my computer) there are two files with .apk extension. One is named "com.companyname.myapp.apk" and the other is named "com.companyname.myapp-Signed.apk". After moving these files to the main folder on my phone I can install only the second one, but after opening it on phone the app is closing immediately. I'm sure that the reason for that is reading file because when I remove the reading method from app, the app is opening normally. Another stranger thing (for me) is that when I plugged the phone into a computer using USB debugging and run the program from a visual studio everything is ok, even if I uninstall app and instal it using the file "com.companyname.myapp-Signed.apk". Here is my reading method:
var path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test.txt");
if (!File.Exists(path))
{
var writer = new StreamWriter(path);
writer.Write("some data");
}
var reader = new StreamReader(path, true);
string data;
data = reader.ReadToEnd();
Above code is calling in constructor in MainPage.xaml.cs fiel, maybe that is important information.
Is it something in my code or the problem is in the project properties. As I read the app doesn't need any permission to works with internal storage, so I have no idea what is wrong.
I'm having a problem while trying to open a PDF file using Process.Start().
The program runs the process as a background process but no adobe reader window show up. A solution i found was to use chrome.exe, but my client want to open on adobe-reader, as it's the default windows program.
(I have also trying without forcing "AcroRd32.exe" and passing the full path of "AcroRd32.exe". Same problem).
Anyone solved this?
My code:
var p = new Process
{
StartInfo = new ProcessStartInfo(#"AcroRd32.exe", #"D:\Tests\81803130000326__.pdf")
};
p.Start();
Background Processes:
I also have checked this question: Process.Start(/* path to pdf */) doesn't work with Adobe Reader on Windows 8
Edits
EDIT INFO: Forgot to say i'm running Windows 10 with Adobe Reader DC 2018.009.20044
EDIT 2: Found a "problem" of Adobe Reader DC. It mantains a level of security that doesn't let me run it as administrator (don't ask me why). Check this link for more information: https://forums.adobe.com/thread/1955530
Now I'll try to find a solution to run it without administrator privileges, or ask my client to uncheck the checkbox
Solution
So, I've found a solution for my problem. The Adobe Reader, since version 10.0, has a "Protected Mode" setting. I wrote a code to change that setting in the registry, so the user won't need to do that. Link: https://www.adobe.com/devnet-docs/acrobatetk/tools/AppSec/protectedmode.html
I hope it helps others in the future!
Solution Code:
var registroAdobe = Registry.LocalMachine.OpenSubKey(#"Software\Policies\Adobe\Acrobat Reader\DC\", true);
registroAdobe.SetValue("bProtectedMode", 0);
This works for Acrobat Reader DC. You can run through "Software\Policies\Adobe\" Products and Versions if you need it.
Actually, I found that this didn't work on my computer, but it led me to look deeper.
I found my solution to be:
var regAdobe = Registry.CurrentUser.OpenSubKey(#"Software\Adobe\Acrobat Reader\DC\Privileged", true);
regAdobe.SetValue("bProtectedMode", 0);
Very similar, but ended up having to edit the user setting rather than the local machine's setting.
Cheers!
You can directly use System.Diagnostics.Process.Start("") to open with default pdfreader program on respective client system.
I have created a Windows form application using C# in visual studio 2010 connecting the database in SQL server . After all my development is done i copy the exe file generated in my machine and pasted in another machine and try to execute it but it is not working .
i can see the process started in task manager but it was closed after 5 seconds . I even tried creating the setup for this application and installed in that machine still i am facing the same issue .
But in my machine it is working perfectly in the both the ways . can any one help me in finding where i went wrong .
Thanks in advance
As you don't provide the error, the answers and comments you are getting are educated guesses.
You should check the event viewer for errors...
This will let you learn what is going on. If you can't fix it, add this info to your question.
As you are not posting exception message, probably you re not properly catching exceptions. Just to be sure surround your main function in a Try/Catch.
In Catch, write some code to dump message exception into a file, or even better use Log4Net. For simplicity just add some code to write to a file now. Something like:
static void Main()
{
try
{
//Your code
}
catch (Exception ex)
{
//Write ex.Message to a file
using (StreamWriter outfile = new StreamWriter(#".\error.txt"))
{
outfile.Write(ex.Message.ToString());
}
}
}
PS: If it is a console application you can survive with Console.Write
Perhaps you have some referenced assemblies that you did not copy along with the application itself.
OR, the connection string is not valid when run from that other machine (if you worked with a local SQL db, or on a network or whatever and it's not accesible on that other machine)
OR, you don't have rights to run it on that other machine.
I have written a web app that has to save a few files. I have developed this app in C# using Visual Web Developer 2010 Express. The program executes flawlessly in VWD but when I deploy it to the server it runs into problems. One problem in particular is that the files being saved are held onto by some process and I can't access them or delete them when I need to. I believe I am properly closing the file streams. Here is an example of one such save:
string[] lines = MessagesTextbox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
FileStream fs2 = File.Open(messagesFileLocation, FileMode.Create, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs2))
{
sw.WriteLine("<Messages>");
foreach (string message in lines)
{
if (!message.Equals(""))
{
sw.WriteLine("\t<message>" + message + "</message>");
}
}
sw.WriteLine("</Messages>");
}
fs2.Close();
The problem is also occurring when I use HtmlAgilityPack to save the rendered HTML to a file.
The only difference between my development environment and the server is that on the server my app runs under IIS. Can anyone think of a reason why this problem might occur using IIS when it doesn't occur ever in my development environment? The person who administers the server thinks it has to be my code but, like I said, it has been running for several weeks on my own machine without any of these problems.
Any suggestions are appreciated.
Regards.
Your FileStream fs2 will not be closed if an Exception is thrown. You must close it in a finally block, which you get for free if you wrap it in its own using.
Everything that implements IDisposable should be wrapped in a using block (or otherwise disposed for some advanced cases) as a matter of good coding practice. There are edge cases to be sure where that does not matter, but it is a solid habit to always ensure that IDisposable.Dispose() is called as soon as an object that implements the interface is no longer required.
I have a Windows Service that hangs when opening an OLEDB-connection to an Excel-file, like this:
using (var connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ fileName + ";Extended Properties=\"Excel 8.0\""))
{
connection.Open();
// start using the connection
}
This code works fine when running as a console application. When I debug the Windows Service with Visual Studio, I can step into the code until I hit the call to connection.Open(). At that point, the thread hangs. No exception is thrown. Visual Studio remains responsive, until I hit the "Break All" or "Stop Debugging" button. At that point, Visual Studio also hangs. When I kill the process, Visual Studio becomes responsive again.
Does anyone know why this happens and how to solve it?
EDIT: fileName is an absolute path; the file was written by the service itself.
After unsuccessfully trying to do this inside the Windows Service, I extracted the business logic into a separate Console Application, and just call that application from within Windows Service. Seems to work fine.
I do not know why this happens, but have you tried to narrow it down by trying to open the file and simply load it into a byte array - to determine whether the issue is with the file system / permissions / etc... rather than OLE DB? If you can open and load the file into a byte array, but OLE DB still hangs, is the CPU pegged which would indicate that there might be something about the file which OLE DB cannot handle?
If you cannot get this to work with OLE DB, have you considered a 3rd party xls / xlsx library like SpreadsheetGear for .NET? You can see live samples here and download a free trial here.
Disclaimer: I own SpreadsheetGear LLC