Click here to Skip to main content
16,016,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, I load a file at the start of a form. I have "Save button" in that form.When I click it, I want to overwrite the file with richtextbox.Savefile method. but I get "Access to path.. is denied"

I checked and got this:
1. Permissions for current user are all granted
2. The debug folder has "Read-Only" -- tried to remove, but they always come back

Now, I think that the program doesn't release the resource(the file) only when I close the form

Is there any method to force this? (I think the file remains loaded into the RAM memory)

One more thing: I must use SaveFile and LoadFile methods. I am working with RTF files and my code is in such a way that this methods do the best job

Thank you

C#
public EditareArticol(string path,List<capitol>chapters,Object[,]lca)
        {
 this.richTextBoxEx1.LoadFile(path, RichTextBoxStreamType.RichText);

      }


private void saveToolStripButton_Click(object sender, EventArgs e)
        {
richTextBoxEx1.SaveFile("articles\\"+textBox1.Text+".dat",RichTextBoxStreamType.RichText);   
File.SetAttributes("articles\\" + textBox1.Text + ".dat", File.GetAttributes("articles\\" + textBox1.Text + ".dat") | FileAttributes.Hidden);       
        }



EDIT 2

I post some code from the method that updates my files:

C#
try
           {
               WebClient request = new WebClient();
               request.Credentials = new NetworkCredential("lucian", "lucian");
               numefisier = numefisier + ".dat";
               byte[] filedata = request.DownloadData(Path.Combine(adresaserver, "/", "c:/ArticoleAplicatieLicenta/", numefisier));

               if (Directory.Exists("articles"))
               {
                   Console.WriteLine("That path exists already.");

               }
               else
               {
                   DirectoryInfo di = Directory.CreateDirectory("articles");
                   di.Attributes |= FileAttributes.Hidden;
                   di.Attributes &= ~FileAttributes.ReadOnly;
               }

               using (FileStream file = File.Create("articles\\" + numefisier))
               {

                   file.Write(filedata, 0, filedata.Length);
                   file.Close();
               }
               File.SetAttributes("articles\\" + numefisier, File.GetAttributes("articles\\" + numefisier) | FileAttributes.Hidden);

               Console.WriteLine("Download complete");
           }
           catch (Exception ee)
           {
               Console.WriteLine(ee.Message);
               //return false;
           }
Posted
Updated 8-May-13 20:33pm
v4
Comments
CHill60 8-May-13 9:26am    
If you post your code for loading the file and for saving it then we might be able to spot what's going wrong - use the Improve question link
Sergey Alexandrovich Kryukov 8-May-13 9:47am    
What "release from method" is supposed to mean?
No, your assumption about releasing resources is wrong. How can it be related to the access to the directory?
—SA
Luci C 8-May-13 10:01am    
Release the handle that is keeping my file locked.. That was the point I was referring to...
Sergey Alexandrovich Kryukov 8-May-13 10:09am    
File handle, yes, maybe, but not the "resource".
—SA
Luci C 8-May-13 10:10am    
Sorry for wrong term... Do you any idea regarding this? How I can release the handle?

Luci C wrote:
Release the handle that is keeping my file locked…
Well, I'm not really sure, but this is what you should check up. Please see my comment to the question in reply to your comment. This is easy easy enough to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
Luci C 8-May-13 10:33am    
I searched wiht handle.exe and found that I have 2 handles from mysql database. But my file is locally, and even if I killed the mysqld process I still cannot save(overwrite) the file
Sergey Alexandrovich Kryukov 8-May-13 10:43am    
Well, keep your investigation. First, run handle.exe again. Maybe you did not kill the process holding a file yet. Check if you can access it "manually" (from Explorer or something). Get to the root of the problem: why the file appeared to be locked in first place. I have limited access to your hard drive these days, but you have it all. :-)
—SA
Luci C 8-May-13 11:11am    
i can't find what is locking it. I tried in another project and it works very well with my initial code. Now I have updated my question with some piece of code that downloads some files. Do you see something that could get stuck in locking state?
Problem SOLVED:
the program didn't let me to overwrite the files because they were having HIDDEN attribute
Once I removed that, all start working great.
Thank you for your help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900