|
Read up on Code Access Security and that'll make much more sense. For example, try running your app from a local hard drive and it'll work. Try running it from a network source, i.e.: mapped network drive, and you'll get security exceptions. Network sources for managed code, by default, are not trusted sources, so they run inside a heavily restricted sandbox.
|
|
|
|
|
Hi everybody!
I'm doing my project and I'm getting this message with just one of the .txt files that I create before.
I made a .dll from a C++ project and it works perfectly. The objective of the DLL is to create three images and three .txt files, after it finishes I call the images, which is working perfectly, and then I call the .txt files. Two of them open perfect and they show me the respective data but when I do click on a button to call the third .txt to show me the data, on the Debug mode VS2005 tells me: "The process cannot access the file TXT because it is being used by another process"
I don't know if it is maybe because the DLL is still making the txt file or something like that, but I haven't found the solution to my issue.
Here is a part of my code that is working with the other two files but it isn't with the last one:
int columnas = 0;
string puntoycoma, array3 = "";
string[] lines = System.IO.File.ReadAllLines(VariablesGlobales.path1 + "results\\correspondencias3.txt");
puntoycoma = lines[0];
int filas = lines.Length;
The part that is in black is where I read the .txt file.
I hope you can help me, I'm getting desperated!!
Thanks a lot for your attention, if you need more information about my code, I'm gonna be looking my mail all the time in case i receive a response from you.
|
|
|
|
|
Did you forget to call Close() on the file stream in the
code that creates the file?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, I'm working with a another guy on my project, he is the one who knows how to work with C++. But while I contact him I can show you what he did:
First he creates the file and a pointer to the file or namefile:
Cmatrix correspondencias1;
correspondencias1.crea_matriz(width, height, 4);
FILE *streamp = fopen("results/correspondencias1.txt", "w");
Then he makes this to fill the file with the matrix columns and rows.
for (y= 0; y<H ; y++)
{
int *current = &m_disparity.Pixel(0, y, 0);
for (x = 0; x < W; x++)
{
correspondencias3.asignar_filas(x,y,current[x]);
if (x==W-1)
fprintf(streamt,"%d,%d %d,%d;\n",correspondencias3.matrix[x][y][0],correspondencias3.matrix[x][y][1],correspondencias3.matrix[x][y][2],correspondencias3.matrix[x][y][3]);
else
fprintf(streamt,"%d,%d %d,%d; ",correspondencias3.matrix[x][y][0],correspondencias3.matrix[x][y][1],correspondencias3.matrix[x][y][2],correspondencias3.matrix[x][y][3]);
}
}
fclose(streamt);
Well I saw the he used the fclose, I guess that this is what you meant, isn't it? streamt is a pointer, so it means that it is closing the file or not?
If you see that something is missing, please tell me and I tell him. I really appreciate your attention. Thanks a lot for your help.
|
|
|
|
|
That's what I meant, thanks.
I see
FILE *streamp = fopen(
and
fclose(streamt );
Is that just a typo?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Oh, I'm sorry, I copied another part of the algorithm that creates one of the files that are working.
this is the parte where he declarates the streamt:
Cmatrix correspondencias3;
correspondencias3.crea_matriz(W,H,4);
FILE *streamt = fopen("results/correspondencias3.txt", "w");
and this is the part that it uses to create the .txt:
for (y= 0; y<H ; y++)
{
int *current = &m_disparity.Pixel(0, y, 0);
for (x = 0; x < W; x++)
{
correspondencias3.asignar_filas(x,y,current[x]);
if (x==W-1)
fprintf(streamt,"%d,%d %d,%d;\n",correspondencias3.matrix[x][y][0],correspondencias3.matrix[x][y][1],correspondencias3.matrix[x][y][2],correspondencias3.matrix[x][y][3]);
else
fprintf(streamt,"%d,%d %d,%d; ",correspondencias3.matrix[x][y][0],correspondencias3.matrix[x][y][1],correspondencias3.matrix[x][y][2],correspondencias3.matrix[x][y][3]);
}
}
fclose(streamt);
the problem is with correspondencias3.txt, correspondencias1.txt and correspondencias2.txt are working perfect.
if this is not the problem, what could it be?
thanks again for your help! I really really appreciate it!
if you need more info, just tell me.
|
|
|
|
|
Some process is holding the file open.
The only thing I can think of is step through the code -
especially from the point the file is opened (fopen()) to
where it gets closed (fclose()).
Make sure there's no execution paths that bypass the fclose().
Also check the return value of fclose().
Make sure there's no typos since you're working with 3 similarly named
sets of objects.
Good luck
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, I did it!
I didn't modify the file that was created by the C++ DLL. What I did was copying the file into another one.
In the C# I put this:
try
{
File.Copy(VariablesGlobales.path1 + "results\\correspondencias3.txt", VariablesGlobales.path1 + "results\\correspondencias4.txt");
}
catch
{
MessageBox.Show("No se pudo leer archivo de texto, favor haga click en Aceptar y espere un momento", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
string[] lines = System.IO.File.ReadAllLines(VariablesGlobales.path1 + "results\\correspondencias4.txt");
I did a new one, a new .txt. That new one was the one that I read and copy to the new array!
Thanks a lot for your help again!
|
|
|
|
|
Thanks,but as per my requirement,without copying the file,i have to access the file.Any solution?please...
Regards,
Kiruthika
|
|
|
|
|
From my experience, the static methods of the "File" class are very slow compared to streams. Maybe 100 times slower. If you have concurrent access to a file it is best to make the reads and writes as short as possible.
I recommend replacing File.ReadAllLines with:
using (StreamReader sr = new StreamReader(...))
{
lines = sr.ReadToEnd();
}
Keyboard not found. Press F1 to continue.
|
|
|
|
|
How do I release the resources for an image so I can rename or delete it.
Bitmap pic = new Bitmap(infile);
pictureBox1.Image = pic;
// A button is clicked to rename or delete the picture
// I have tried
pic=null;
pictureBox1.Image=null;
File.Move(inpic, outpic);
//I get ************** Exception Text **************
//System.IO.IOException: The process cannot access the file because it is
//being used by another process.
I have seen this issue dealt with before but have not been able to find where.
|
|
|
|
|
The problem is that the picture was never Disposed. There are also bugs in the framework, I find the best thing is to create the bitmap, make a copy, work with the copy, and call Dispose on the original object. Setting to null doesn't do much, although it can increase the time before GC collects it, there's no immediate effect on the object.
Christian Graus
Driven to the arms of OSX by Vista.
"Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
|
|
|
|
|
electriac wrote: How do I release the resources for an image so I can rename or delete it.
Dispose it!
pic.Dispose();
pic=null;
GDI+ keeps the image stream open until you dispose the image.
It's buried way deep in the documentation - like a one sentence
blurb in an overview somewhere.
You can work with a clone as well, like Christian mentioned.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks Mark that did the trick!
|
|
|
|
|
Hey there, it was my post about a month ago you're talking about Glad you found the solution , One more problem is in the pictureBox control "when loading it with a bitmap", more details are on msdn website..
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
Do you have a URL for that I was not able to find it with a search.
|
|
|
|
|
|
Muammar
Thanks for the link.
I am doing a rewrite of my media player which displays images associated with the MP3 files. Since the program also deletes, renames, and exports groups of files I have had some difficulty do to the problem of unreleased resources. I believe that with the help of all you "Code Project" people I have everything working now.
Thanks to all
Electriac
|
|
|
|
|
I am certain that there have been times that I've disposed of the image and it's still held onto the file, depending on how it was opened. But, yeah, as we both said, Dispose is the underlying solution that the OP was missing.
Christian Graus
Driven to the arms of OSX by Vista.
"Iam doing the browsing center project in vb.net using c# coding" - this is why I don't answer questions much anymore. Oh, and Microsoft doesn't want me to.
|
|
|
|
|
It seems that MS doesn't want anything help full, altruistic, or practical. Thanks for the help.
|
|
|
|
|
Christian Graus wrote: I am certain that there have been times that I've disposed of the image and it's still held onto the file
Yup - I've seen this. It seems to depend (to a certain extent) on how the image was opened in the first place.
|
|
|
|
|
Pete O'Hanlon wrote: It seems to depend (to a certain extent) on how the image was opened in the first place.
That makes sense. Disposing the Bitmap to make it release the file only works if it's actually the Bitmap that is holding on to the file.
The Bitmap should always be disposed, of course, even if it's not to release the file.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Yeah "Deja View" I know I have seen this issue before but after spending many hours with Google etc. to no avail I resorted to asking for help. Many thanks Code Project. I try not to ask questions unless I have exhausted all alternatives. Thanks for your help I now have greater insight into the issue than my simple problem required.
|
|
|
|
|
electriac wrote: I know I have seen this issue before but after spending many hours with Google etc
That's the spirit. BTW - don't think that Deja View referred to your problem. It's part of my sig.
|
|
|
|
|
I did think it was a cute reference to my inability to find the previous post about picture boxes.
|
|
|
|
|