944,106 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 553
  • C# RSS
Nov 6th, 2009
0

Retrieveing binary data

Expand Post »
I did like:

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
Try,
C# Syntax (Toggle Plain Text)
  1. FileStream fs = new FileStream(System.IO.Path.GetTempPath() + @"\MyTempDir\" + listBox1.SelectedValue, FileMode.Create);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 6th, 2009
0
Re: Retrieveing binary data
Click to Expand / Collapse  Quote originally posted by adatapost ...
Try,
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
Got it
C# Syntax (Toggle Plain Text)
  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");
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
This is the same like I did in my 1st upper example.
Correct is:
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
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 )

C# Syntax (Toggle Plain Text)
  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; Nov 6th, 2009 at 8:12 am.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
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:
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
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

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 6th, 2009
0
Re: Retrieveing binary data
Big thanks. IT works great.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Binding data to the custom DataGridView
Next Thread in C# Forum Timeline: Matrix problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC