UnauthorizedAccessException

P

pterodactyl76

Hi guys.
I run sample code on Pocket PC 2003 emulator, that use cf 2.0
the code opens "big" file and write parts of this file to another
files.
program can write 1, 10, 20, 70..any files and then shows error
message while creating next file:
System.UnauthorizedAccessException: UnauthorizedAccessException
× System.IO:__Error:WinIOError()
× System.IO:FileStream:.ctor()
× System.IO:FileStream:.ctor()
× PagesDB:MainForm:MakeInstall()

here is the code:
private void MakeInstall()
{

System.IO.FileStream fs = null;

BinaryReader br = null;

try
{
String sLocation = "\\Storage card\\Data\\";

fs = new FileStream(sLocation + "Data.x",
FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);

int l = System.BitConverter.ToInt32(br.ReadBytes(8),
0);

MemoryStream ms = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(ms);
outZStream.Write(br.ReadBytes(l), 0, l);
outZStream.Flush();
outZStream.Close();


ms.Seek(0, SeekOrigin.Begin);
ArrayList al = new ArrayList();
String str=String.Empty;

using (StreamReader sr = new StreamReader(ms,
System.Text.Encoding.GetEncoding(1251)))
{
while ((str = sr.ReadLine()) != null)
{
al.Add(str);
}
}

ms.Close();

if (!Directory.Exists(sLocation + "IDX\\"))
{
Directory.CreateDirectory(sLocation + "IDX\\");
}

String name = String.Empty;
System.IO.FileStream fs1 =null;

for (int i = 0; i < al.Count; i++)
{
name = sLocation+((String)al).Split('|')[0];
l =
System.Convert.ToInt32(((String)al).Split('|')[1]);

if (File.Exists(sLocation + name))
File.Delete(sLocation + name);

//fs1 = File.OpenWrite(name);//here is error
//fs1 = new FileStream(name, FileMode.CreateNew);//
same error
fs1 = new FileStream(name, FileMode.Create);//
error too
fs1.Write(br.ReadBytes(l), 0, l);
fs1.Flush();
fs1.Close();

}
}
catch (Exception err)
{
throw new Exception("Error in module MakeInstall " +
err.Message);
}
finally
{
if(br!=null)
br.Close();
}

}

i use local administrator account, the full rights on emulator's
storage card folder grants to everyone, system and users. i check file
and folder properties - sometimes they became readonly, but in program
is no code that makes it, just code that run MakeInstall procedure.
the folders are empty. if they not empty program write some files, and
throws error, next run it write more files and throws error, every
next run it throws eroro and write more files.

Can somebody explain, why this happens?

WBR
Kirill
 
D

DrewCE

Try changing...

if (File.Exists(sLocation + name))
File.Delete(sLocation + name);
....to...
if (File.Exists(name))
File.Delete(name);

-Drew

Hi guys.
I run sample code on Pocket PC 2003 emulator, that use cf 2.0
the code opens "big" file and write parts of this file to another
files.
program can write 1, 10, 20, 70..any files and then shows error
message while creating next file:
System.UnauthorizedAccessException: UnauthorizedAccessException
× System.IO:__Error:WinIOError()
× System.IO:FileStream:.ctor()
× System.IO:FileStream:.ctor()
× PagesDB:MainForm:MakeInstall()

here is the code:
private void MakeInstall()
{

System.IO.FileStream fs = null;

BinaryReader br = null;

try
{
String sLocation = "\\Storage card\\Data\\";

fs = new FileStream(sLocation + "Data.x",
FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);

int l = System.BitConverter.ToInt32(br.ReadBytes(8),
0);

MemoryStream ms = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(ms);
outZStream.Write(br.ReadBytes(l), 0, l);
outZStream.Flush();
outZStream.Close();


ms.Seek(0, SeekOrigin.Begin);
ArrayList al = new ArrayList();
String str=String.Empty;

using (StreamReader sr = new StreamReader(ms,
System.Text.Encoding.GetEncoding(1251)))
{
while ((str = sr.ReadLine()) != null)
{
al.Add(str);
}
}

ms.Close();

if (!Directory.Exists(sLocation + "IDX\\"))
{
Directory.CreateDirectory(sLocation + "IDX\\");
}

String name = String.Empty;
System.IO.FileStream fs1 =null;

for (int i = 0; i < al.Count; i++)
{
name = sLocation+((String)al).Split('|')[0];
l =
System.Convert.ToInt32(((String)al).Split('|')[1]);

if (File.Exists(sLocation + name))
File.Delete(sLocation + name);

//fs1 = File.OpenWrite(name);//here is error
//fs1 = new FileStream(name, FileMode.CreateNew);//
same error
fs1 = new FileStream(name, FileMode.Create);//
error too
fs1.Write(br.ReadBytes(l), 0, l);
fs1.Flush();
fs1.Close();

}
}
catch (Exception err)
{
throw new Exception("Error in module MakeInstall " +
err.Message);
}
finally
{
if(br!=null)
br.Close();
}

}

i use local administrator account, the full rights on emulator's
storage card folder grants to everyone, system and users. i check file
and folder properties - sometimes they became readonly, but in program
is no code that makes it, just code that run MakeInstall procedure.
the folders are empty. if they not empty program write some files, and
throws error, next run it write more files and throws error, every
next run it throws eroro and write more files.

Can somebody explain, why this happens?

WBR
Kirill
 
P

pterodactyl76

sLocation variable is a path to files folder
name - just name of the file, like "objectpnpr10721088", or "IDX\
\objectpnpr10721088"
yes, it was a bug:) but error still occurs, also sometimes emulator
can't see files and folders in storage card.
looks like emulator issue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top