Why sometimes when i click a button to save the file again the file is access denied ? Not used by a

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Maybe i have to release the file after loading it ?

This is in Form1 the Load event:

<pre class="prettyprint private void loadCoordinatesToolStripMenuItem_Click(object sender, EventArgs e)
{
file1 = "";
DialogResult result1;
result1 = new DialogResult();
openFileDialog1.Title = "Select a text file to load";
openFileDialog1.InitialDirectory = @"C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASE";
openFileDialog1.FileName = null;
openFileDialog1.Filter = "Text File|*.txt";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
result1 = openFileDialog1.ShowDialog();
if (result1 == DialogResult.OK)
{
FileInfo fi;
file1 = openFileDialog1.FileName;
fi = new FileInfo(file1);
string t = fi.DirectoryName;
string fn = fi.Name;
string noExtenstion = Path.GetFileNameWithoutExtension(fn);
wireObject1 = new WireObject(fn);
wireObject1.Load(t);
locked(wireObject1.lockObject);
textBox3.Enabled = false;
trackBar1.Enabled = false;
saveBaseFileToolStripMenuItem.Enabled = true;
textBox3.Text = noExtenstion;
pictureBox1.Refresh();

}
if (result1 == DialogResult.Cancel)
{
}

}[/code]
This is the save event in Form1:

<pre class="prettyprint private void saveBaseFileToolStripMenuItem_Click(object sender, EventArgs e)
{
string path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
wireObject1.Save(path_exe,Lock,pictureBox1);
}[/code]
And this is where im getting the problem from time to time:

<pre class="prettyprint public void Save(string path , bool Locked , PictureBox pb)
{
string fn;
string t = Path.GetFileNameWithoutExtension(wo_name);
if (File.Exists(path + "\" + "DATABASE" + "\" + t + "\" + wo_name))
{
string f = Path.Combine(path + "\" + "DATABASE" + "\" + t + "\" + wo_name);
File.Delete(f);
fn = path + "\" + "DATABASE" + "\" + t + "\" + wo_name;
}
else
{
fn = path + "\" + "DATABASE" + "\" + wo_name + "\" + wo_name + ".txt";
}
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("Button Lock", Locked.ToString());

setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);

setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);


}[/code]
Not sure why now i have a text file and its still locked i cant load it. Even on my hard disk with notepad.
Now i tried to load it with notepad got error need admin rights exited and the file deleted.

Im not sure where the problem is why sometimes the file is under admin rights needed.

The file i save it under: C:UsersmyuserAppDataLocalAnimationEditorAnimationEditorDATABASETest
Never had problems before saving files there.
Im not sure if the problem is when im loading the file or maybe when saving and then loading again.
Maybe i need to release somehow the file after loading or saving ?


<hr class="sig danieli

View the full article
 
Back
Top