Re: UnauthorizedAccessException

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Try changing...

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

-Drew

<pterodactyl76@xxxxxxxxx> wrote in message news:eccb90ad-b696-4caa-b833-4e03fac70401@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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[i]).Split('|')[0];
l =
System.Convert.ToInt32(((String)al[i]).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

.



Relevant Pages

  • Re: VB6 and Windows Media Player
    ... within the My Music Folder. ... Private Declare Function ShellExecute Lib "shell32.dll" Alias ... "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal ... the folder Documents and Settings eg:- C:\Documents and Settings? ...
    (microsoft.public.vb.general.discussion)
  • Re: File Input Question
    ... To have a user select a folder, not a file, I use the below. ... pszDisplayName As String 'Address of a buffer to receive the display name of the folder ... Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long ... Private Declare Function GetWindowRect Lib "user32" ...
    (microsoft.public.excel.programming)
  • Re: Execute macro for all documents in the folder
    ... It is simple enough to batch process a single folder. ... Dim strFileName As String ... It seems as though the below macro from an earlier posting was put ...
    (microsoft.public.word.docmanagement)
  • Re: Execute macro for all documents in the folder
    ... It is simple enough to batch process a single folder. ... Dim strFileName As String ... It seems as though the below macro from an earlier posting was put ...
    (microsoft.public.word.docmanagement)
  • Re: Declare Sub Sleep Lib "kernel32.dll" ( ByVal dwMilliseconds As Lon
    ... Sub MovePrintedFile(ByVal sFileName As String) ... Dim sDestinationFolder As String ... you can use Windows API commands to create the folder. ... Private Type SHFILEOPSTRUCT ...
    (microsoft.public.word.vba.general)