Retrieveing binary data

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training

Retrieveing binary data

 
0
  #1
27 Days Ago
I did like:

  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir" + listBox1.SelectedItem", FileMode.Create);
  2.  
And you know what happens? It partly works. It opens the files (if its a pdf file, it opens in a acrobat reader, if doc it opens in word,...) but there is no file in that directory. listBox1.SelectedItem hold the full name of the file (like: "myFile.doc").

Strange, why is not there? The code go through well, it creates the file, but the file it is not shown in the specified (MyTempDir) folder.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

If I just simply declare the file name in the code like:

  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir" + @"\myFile.doc", FileMode.Create);
It again creates the file but this time it even phisicly appears in the specified (MyTempDir) folder.

Why is there the difference between these two example?



Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #2
27 Days Ago
Try,
  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir\" + listBox1.SelectedValue, FileMode.Create);
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #3
27 Days Ago
Originally Posted by adatapost View Post
Try,
  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir\" + listBox1.SelectedValue, FileMode.Create);
This is the same like I did in my 1st upper example.
Correct is:
  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir\" + "\\" + listBox1.SelectedValue, FileMode.Create);

I would like to know something else.

I am doing the code, where are now stored temporary files, which need to be deleted when a user runs this application for the 2nd time (1st time there is no directory and no files in it).

For the 2nd time let`s presume there are files inside "MyTempDir".
So how to delete them at the 1st place?
I was trying to delete the whole folder but it throws an error, there are files inside this folder. So I defenatelly have to delete all files inside it.

How?
This code I have now:
  1. if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + @"\MyTempDir"))
  2. {
  3. System.IO.Directory.Delete(System.IO.Path.GetTempPath() + @"\MyTempDir"); //I CAN NOT DELETE FOLDER IF THERE ARE FILES INSIDE OF IT
  4. System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + @"\MyTempDir");
  5. }
  6. else
  7. System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + @"\MyTempDir");

So how to delete files? And I do not know what kind of files I got inside.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #4
27 Days Ago
Got it
  1. if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + @"\MojaMapa"))
  2. {
  3. string[] datoteke = Directory.GetFiles(System.IO.Path.GetTempPath() + @"\MojaMapa");
  4. foreach (string datoteka in datoteke)
  5. {
  6. File.Delete(datoteka);
  7. }
  8. }
  9. else
  10. System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + @"\MojaMapa");
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 375
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 66
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
0
  #5
27 Days Ago
Originally Posted by Mitja Bonca View Post
This is the same like I did in my 1st upper example.
Correct is:
  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir\" + "\\" + listBox1.SelectedValue, FileMode.Create);
The code adatapost gave you wasn't the same as your original, or did you mean it gave the same result?

You need to be careful when accessing the selected item/value in a control. It is a common mistake, people expect the SelectedItem to return whatever the string in the control is but sometimes it will return something along the lines of "System.Windows.Controls.ComboBoxItem" :p

Also, rather than delete the files when you load the application, if they arent needed after the app closes, you could always delete them when the app closes
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #6
27 Days Ago
I know, and I am sorry for saying that. I noticed a bit too late (after 30 mins, which is the limit for editing posts). So I couldn`t change it.

I saw a bit later he added a sing "\" on the end of "TempDir".

Sorry ones again, really didnt see it.

But I do have another small problem.

I did the upper code which creates a temp folder in which will be saved files, retrieved from the database. But I want to delete these files and the best I could think of is to delete files in the next session (when user re-opens this app.). This code does this, but I have a problem, if there is one of these files still opened (the file uses another process).

How can I do the code for letting the user know, that if he want to run the application again , he has to close that file (if he opeded the file myFile.pdf, he has to close this file which is running in acrobat Reader )

  1. if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + @"\MyTempDir"))
  2. {
  3. string[] files = Directory.GetFiles(System.IO.Path.GetTempPath() + @"\MyTempDir");
  4. foreach (string file in files)
  5. {
  6. //HERE I AM MISSING AN ERROR MESSAGE!
  7. File.Delete(file);
  8. }
  9. }
  10. else
  11. System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + @"\MyTempDir");

And it is a good idea of deleteing them on app close () event.
But still I can not delete file, if this is opened, righ? So, please for some help. And consider that I do not know which file, maybe there are more files opened.
Last edited by Mitja Bonca; 27 Days Ago at 8:12 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #7
27 Days Ago
I got an error on ref : A parameter cannot have all the specified modifiers; there are too many modifers on the parameter.
This is my code:
  1. private void MyForm_Load(object sender, EventArgs e)
  2. {
  3. if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + @"\MyFolder"))
  4. {
  5. string myFile;
  6. string[] allFiles = Directory.GetFiles(System.IO.Path.GetTempPath() + @"\MyFolder");
  7. foreach (string file in allFiles)
  8. {
  9. IsFileOpen(out myFile);
  10. }
  11. }
  12. else
  13. System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + @"\MyFolder");
  14. }
  15.  
  16. public bool IsFileOpen(out ref string file) //ref is here the problem, any clues why?
  17. {
  18. if ((File.GetAttributes(file) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
  19. {
  20. using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
  21. {
  22. try
  23. {
  24. stream.ReadByte();
  25. return false;
  26. }
  27. catch (IOException)
  28. {
  29. return true;
  30. }
  31. finally
  32. {
  33. stream.Close();
  34. stream.Dispose();
  35. File.Delete(file);
  36. }
  37. }
  38. }
  39. else
  40. return true;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 375
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 66
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
0
  #8
27 Days Ago
No worries, it happens :p I was just checking to see if you meant you were getting the same problem.

Checking if the file is open is unfortunately not a simple thing to do. There is always the chance of a race condition (its not open when you check but gets opened before you delete it).

Your best bet is to use a try/catch. Ive included a while loop so it will continue to retry after the user has been notified. Be aware though, that this will mean the application will keep trying until the file is deleted. You could give the messagebox OK and Cancel buttons then capture the Cancel button to skip the file, but this is jsut a simple example for you

  1. foreach (string file in files)
  2. {
  3. while (File.Exists(file))
  4. {
  5. try
  6. {
  7. File.Delete(file);
  8. }
  9. catch (IOException ex)
  10. {
  11. MessageBox.Show("Error deleting file: " + file + Environment.NewLine
  12. + "Please close all copies of the file before continuing");
  13. }
  14.  
  15. }
  16. }
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #9
27 Days Ago
Big thanks. IT works great.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC