Click to See Complete Forum and Search --> : A SaveFileDiaolog Problemm!!!Help needed


AHungry_Mind
May 19th, 2005, 06:13 AM
I am getting trouble with saveFileDialog :( I could not find the mistake:(.Actually program works ;but when i savefileDialog appears and i write the name of the file when i press the save button it throws an exception and says that:
The process can not access the file "bla bla\My Documents\asd.txt" because it is being used by another process and your access denied .
After this error when i try to debug with the code breakpoints even Vs .Net gone mad and gives some errors like you can not access the phonebook.exe or cs.
My saveFileDialog looks like this:
private void saveAsItm_Click(object sender, System.EventArgs e)
{
check:
System.IO.Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//ArrayList arr =new ArrayList();
saveFileDialog1.Filter = "Txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.Title="Save as";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
try
{
if(saveFileDialog1.FileName.EndsWith("txt"))
{

System.IO.StreamWriter sw=new System.IO.StreamWriter(saveFileDialog1.FileName);
for(int i=0;i<PersonList.Capacity;i++)//PersonList is a global ArrayList
{
sw.Write((PersonList[i]).ToString());//Writes the contents of
sw.Close();
//myStream.Close();
}
}
else {
MessageBox.Show("Error: Check if you entered the file extension properly or not","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
goto check;
}
MessageBox.Show("Your list saved.");
}
catch(Exception e1)
{
MessageBox.Show("Error: "+e1.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
myStream.Close();
}
}

cilu
May 19th, 2005, 07:27 AM
That's becuase you are trying to open it twice.

First, you open the file here

if((myStream = saveFileDialog1.OpenFile()) != null)

and before closing this stream ( which atually you never do) you open it for a second time

System.IO.StreamWriter sw=new System.IO.StreamWriter(saveFileDialog1.FileName);

What is the logic of your code?

PS: and please use code tags (see the Code button).

AHungry_Mind
May 20th, 2005, 01:58 AM
cilu thanx for your reply.I've changed the code and last state of code is as below:

private void saveAsItm_Click(object sender, System.EventArgs e)
{
check:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string fileName;
saveFileDialog1.Filter = "Txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.Title="Save as";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName=saveFileDialog1.FileName;//At this place i am getting the filename that the user entered in the saveDialogFile

try
{

if(saveFileDialog1.FileName.EndsWith("txt"))
{
//output = new FileStream( fileName ,FileMode.OpenOrCreate, FileAccess.Write);
System.IO.StreamWriter sw=new System.IO.StreamWriter(saveFileDialog1.FileName);
for(int i=0;i<PersonList.Capacity;i++)//PersonList is a global ArrayList and has Person Objects in it
{
sw.WriteLine((PersonList[i]).ToString());//At this line i am writing the contents of the PersonList (which is composed of name ,surname and phone number)
}
}
else
{
MessageBox.Show("Error: Check if you entered the file extension properly or not","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
goto check;//If the extension didn't written display SaveDialogFile again
}
MessageBox.Show("Your list saved.");
}
catch(Exception e1)
{
MessageBox.Show("Error: "+e1.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

}
}

but this time i am getting that exception:
System.ArgumentOutOfRangeException : Index was out of range and must be non negative.
I think this is about PersonList.Capacity ;but i could not fix the bug.
Briefly the logic of the code is when the user clicks to save item at the menu (saveDialogFile) he/she chooses the location of the place where he/she wants to save the txt file and this txt file is composed of PersonList which is ArrayList of Person objects.I

cilu
May 20th, 2005, 03:38 AM
Which line throws this exception? I bet is this one:

for(int i=0;i<PersonList.Capacity;i++)
{
sw.WriteLine((PersonList[i]).ToString());
}

You should not use Capacity for this. Capacity tells you the number of elements the container (array, list, etc.) can hold without reallocating space.

Count tells you how many elements are in the container.

MSDN for ArrayList

Capacity is the number of elements that the ArrayList is capable of storing. Count is the number of elements that are actually in the ArrayList.

Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity of the list is doubled by automatically reallocating the internal array.

When the value of Capacity is set explicitly, the internal array is also reallocated to accommodate the specified capacity. If Capacity is explicitly set to zero, the common language runtime sets it to the default capacity instead. The default capacity is 16.