从RichtextBox LoadFile方法发布文件 [英] Release file from RichtextBox LoadFile Method

查看:243
本文介绍了从RichtextBox LoadFile方法发布文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在表单的开头加载一个文件。我在该表单中有保存按钮。当我单击它时,我想用richtextbox.Savefile方法覆盖该文件。但我得到访问路径..被拒绝



我检查并得到了这个:

1.当前用户的权限都是授予

2.调试文件夹有只读 - 试图删除,但它们总是回来



现在,我认为只有当我关闭表格时程序才会释放资源(文件)



有什么方法可以强行执行此操作吗? (我认为文件仍然加载到RAM内存中)



还有一件事:我必须使用SaveFile和LoadFile方法。我正在使用RTF文件和我的代码是这样的方法,这种方法做得最好



谢谢



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

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);       
        }







编辑2



我发布了更新文件的方法中的一些代码:






EDIT 2

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

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;
           }

推荐答案

Luci C写道:

释放保持的句柄我的文件已被锁定...

Release the handle that is keeping my file locked…

嗯,我不太确定,但这是你应该检查的。请在回复您的评论时查看我对该问题的评论。这很容易调查。



首先,类似的问题在这里被多次询问,根据这一经验我知道:在大多数情况下阻塞过程是你自己的过程。您可能忘记在同一个应用程序中处理/关闭某些内容。所以,首先,检查一下。要探索这种可能性,请参阅我过去的答案:

清除C#中的句柄 [ ^ ]。



在这个答案中,注意使用 / code>语句可以帮助您保证在使用后正确处理相应的文件系统对象,而不是保持文件锁定。



在同样的情况下,你真的需要调查哪个进程拥有哪个文件。为此,我建议使用 Sysinternals Suite 中的一个实用程序。这组实用程序(以前来自 Winternals 公司,目前在Microsoft)是必须的对于任何开发人员,请参阅:

http://technet.microsoft.com/en -us / sysinternals / bb842062 [ ^ ],

http://technet.microsoft.com/en-us/sysinternals / bb545027 [ ^ ]。



您需要的实用程序是 handle.exe ,请参阅:

http://technet.microsoft.com/en-us/sysinternals/bb896655 [ ^ ]。



在您的情况下,您使用文件名参数:

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>





此实用程序将扫描所有类型的句柄,而不仅仅是文件句柄。对于文件,它将扫描与文件名匹配的所有文件句柄(因此它不必是完整路径名)并返回足以识别每个进程的信息,包括其 pid 。因此,如果您需要有关相关流程的更多信息,您还可以使用其他Sysinternals实用程序,特别是其 Process Explorer

http://technet.microsoft.com/en-us/sysinternals/bb896653 [ ^ ]。



祝你好运,

-SA



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


问题已解决:

程序没让我覆盖文件,因为他们有HIDDEN属性

一旦我删除它,一切都开始工作了。

谢谢你的帮助
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


这篇关于从RichtextBox LoadFile方法发布文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆