GeckoFX和C#!单击按钮删除文件时出错

时间:2012-08-05 02:26:30

标签: c# geckofx

组合GeckoFX和C#时遇到2个问题。

1.当我点击一个按钮时,我的应用程序将打开一个OpenFileDialog(由C#代码生成)来更改img标签的src属性。我使用这个img的contextmenu来做到这一点。我的问题是,如果我点击按钮打开OpenFileDialog之后,我点击img(没有contextmenu)再次打开OpenFileDialog。

2.当我为这个img选择新图像时,我无法删除旧文件(使用C#代码) 这是我的代码

[HTML和Javascript代码]

<script type="text/javascript">
$(document).ready(function(){
    $('.div_image).bind('contextmenu',function(){
        $('#contextmenu_image').css({top: e.pageY+'px',left: e.pageX+'px'}).show();
    });

});
</script>
<div class="div_image" style="position: absolute; top: '20px;left:'20px;"><img id="img123" class="image" src="" style="width: 100%;height: 100%;"/></div>
<ul class="contextmenu" id="contextmenu_image" style="width: 100px; display: none;">
    <li class="properties">Properties</li>
    <li class="del">Delete</li>
    <button id="choose_image">Choose image</button> 
</ul>

[C#代码]

private void ChooseImage()
    {
        if (geckoWebBrowser1.Document.ActiveElement.GetAttribute("id") == "choose_image")
        {

            OpenFileDialog open = new OpenFileDialog();
            open.Filter =
                "Image (*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG)|*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG|" +
                "All files (*.*)|*.*";
            open.Title = "Choose an image";

            DialogResult result = open.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string srcFile = open.FileName;
                string fileName = System.IO.Path.GetFileName(srcFile);
                string fileExtent = System.IO.Path.GetExtension(srcFile);
                string desDir = Application.StartupPath + "\\test\\images\\";
                Random r = new Random();
                string newFileName = "i_";
                for (int i = 1; i <= 10; i++)
                {
                    newFileName += Convert.ToString(r.Next(0, 9));
                }
                newFileName += fileExtent;
                System.IO.File.Copy(srcFile, desDir + newFileName);
                //Find old image
                string old_image = geckoWebBrowser1.Document.GetElementById("img123").GetAttribute("src");
                geckoWebBrowser1.Document.GetElementById("img123").SetAttribute("src", "images/" + newFileName);
                 if (old_image != "")

                    System.IO.File.Delete(desDir + old_image);//Delete old file,but unable



            }
        }
    }
 private void geckoWebBrowser1_DomClick(object sender, GeckoDomEventArgs e)
    {
        ChooseImage();
    }

抱歉,因为我的英语不好

1 个答案:

答案 0 :(得分:1)

对于您的第一个问题,我建议您更改点击事件的方式:

browser.OnBrowserClick + = new System.EventHandler(OnBrowserClick);

然后,你会得到一个参数,告诉你点击了什么:

private void OnBrowserClick(object sender, EventArgs e)
{
    var ge = e as GeckoDomEventArgs;
    if (ge.Target.ClassName =="choose_image")
    {
       //Handle the click...

对于你的第二个问题,我想也许浏览器正在抓住文件,但在我的实验中,它没有。我建议你确保文件确实存在:

var oldPath = Path.Combine(desDir);
if(File.Exists(oldPath))
{
    try
    {
        File.Delete(oldPath);
    }
    catch(Exception error)
    {
        //do something about not being able to delete the file yet
    }
}

如果您想查看一些使用geckofx执行大量此类操作的开源代码,请参阅我的Bloom project,特别是EditingView.cs和Browser.cs。