7 Replies - 9335 Views - Last Post: 12 December 2008 - 03:35 AM Rate Topic: -----

#1 Michael S   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Beginner's problem with FileStream

Post icon  Posted 10 December 2008 - 02:59 AM

Am a total beginner to C# and am trying to learn via Jack Purdum's C# 3.0 book. I have come to a chapter that explains Using Disk data files and cannot (for the life of me) get the example code to work (which I've keyed in manually. Here is the code that is failing
	public int Create(string fn)
	{
	  try
	  {
		FileStream myFile = new FileStream(fn, FileMode.OpenOrCreate);
		bw = new BinaryWriter(myFile);
		fileName = fn;
	  }
	  catch (FileNotFoundException ex)
	  {
		errorMessage = ex.Message;
		return 1;
	  }
	  catch (IOException ex)
	  {
		errorMessage = ex.Message;
		return 1;
	  }
	  catch (Exception ex)
	  {
		errorMessage = ex.Message;
		return 1;
	  }
	  {
		return 1;
	  }
	  return 0;
	}


I have added the various catches myself to try and figure out what is failing (my code fails on the FileStream call and I enter the exception catch). The content of errorMessage is

Quote

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

The value in fn is

Quote

c:\temp\friends.bin

I have tried 2 values in fn, and both fail. As a newbie, I'm completely stumped. Any help/suggestions appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner's problem with FileStream

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 04:07 AM

I pared your code down to
		public int Create(string fn)
		{
			try
			{
				FileStream myFile = new FileStream(fn, FileMode.OpenOrCreate);
				bw = new BinaryWriter(myFile);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return 1;
			}
			return 0;
		}


which works when I call Create() with "temp.txt" as the file path argument. When I use your string "c:\temp\friends.bin" I get an error: "Illegal characters in path". The reason is that the file path parsing the system does requires that the "\" path separators be doubled up, since they function as an escape character in strings. So your file path passed in through variable fn should be "c:\\temp\\friends.bin".
Was This Post Helpful? 1

#3 Michael S   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 04:20 AM

Thanks for the reply. I changed the code to
   public int Create(string fn)
	{
	   try
	  {
		fn = "text.txt";
		myFile = new FileStream(fn, FileMode.OpenOrCreate);
		bw = new BinaryWriter(myFile);
		fileName = fn;
		etc etc


but that still failed. I'll append the main code so, hopefully, it makes a bit more sense. In the main object code (terminology ?), I have
public class FrmMain : Form
{
  const string TESTDATAFILE = "c:\\temp\\friends.bin";

  long recs;
  long currentRecord = 1;
  clsRandomAccess myData = new clsRandomAccess(TESTDATAFILE);


and then I call my (failing) class with
if (myData.Open(TESTDATAFILE) == 0)


so the double backslashes should (?) be okay.
The open method contains
   public int Open(string fn)
	{
	  if (bw == null)
	  {
		return Create(fn);
	  }
	  else
	  {
		myFile = new FileStream(fn, FileMode.OpenOrCreate);
	  }
	  return 0;
	}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 06:16 AM

Short answer is your program apparently doesn't have permissions to write to the c:\temp directory, or to open friends.bin. Check your permissions on that directory.
Was This Post Helpful? 0
  • +
  • -

#5 Michael S   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 06:23 AM

You're obviously "partly correct. I managed to capture the attached screen. Note that I can quite happily create any file in the directories specified using other products so I'm wondering if there is some sort of "one-off" command I need to perform for c# "files"

Also found the following info when selecting one of the options in the screen capture (if this says anything to someone else)

Quote

System.Security.SecurityException was caught
Message="Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
Source="mscorlib"
StackTrace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path)
at FrmMain.WriteToFile(String file, String msg)
InnerException:

Attached image(s)

  • Attached Image

This post has been edited by Michael S: 10 December 2008 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 Michael S   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 07:07 AM

Could the following screen capture indicate my problem ?

If it does, how do I get the FileIOPermission to show a green tick (assuming that's what's needed). If I select the pulldown and select Incluse, I get a yellow exclamation mark instead (which I assume isn't enough)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Beginner's problem with FileStream

Posted 10 December 2008 - 08:38 AM

Honestly, I know nothing about permissioning and ClickOnce applications. If this is just learning, you could try making it a full-trust application, or turn off the Enable ClickOnce Security Settings.
Was This Post Helpful? 0
  • +
  • -

#8 Michael S   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-December 08

Re: Beginner's problem with FileStream

Posted 12 December 2008 - 03:35 AM

A colleague at work helped me with the problem. Turned out that since the debug exe program was being run from the C-drive and my project was on a network drive, Visual Studio didn't like it (as the message intimated - something to do with security). As soon as we copied the project to the C-drive, the problem disappeared and I could create a file either on the C-drive or the network one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1