Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要加载文件c的组件属性#_C#_Winforms_Windows Forms Designer - Fatal编程技术网

C# 要加载文件c的组件属性#

C# 要加载文件c的组件属性#,c#,winforms,windows-forms-designer,C#,Winforms,Windows Forms Designer,我创建了一个自定义组件,现在我正在尝试使用属性面板上显示的属性 我尝试使用一个文件对话框,以便更轻松地为属性加载一个文件路径 [Browsable(true), DefaultValue(@"Firmware\fx3.img"), Description("Specifies the the location of the Firmware file.")] [EditorAttribute(typeof(System.Windows.Forms.Design.FileNameE

我创建了一个自定义组件,现在我正在尝试使用属性面板上显示的属性

我尝试使用一个文件对话框,以便更轻松地为属性加载一个文件路径

    [Browsable(true), DefaultValue(@"Firmware\fx3.img"), Description("Specifies the the location of the Firmware file.")]
    [EditorAttribute(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string FwFile
    {
        get { return fwfile; }
        set
        {
            fwfile = value;
        }
    }
通过这种方法,它为我提供了无法使用的绝对路径

我要加载的文件是嵌入式资源,正在复制到此路径下的输出目录,例如“Firmware\fx3.img”(相对于输出目录)。 这可能会改变,这就是为什么我要显式选择文件


有比写下相对路径更好的方法吗?

您可以对FileNameEditor类进行子类化,以使其结果符合您尝试执行的操作:

    public class MyFileNameEditor : System.Windows.Forms.Design.FileNameEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var fullname = base.EditValue(context, provider, value);

            if (!string.IsNullOrEmpty(fullname)){
                /// modify Fullname here to return relative path
            }
            return fullname;
        }
    }

它可以更改,但是否始终如此。\Firmware\fx3.img?只需使用。\Filename来限定它,例如fx3v2.img。例如,可以增加一些内容来添加版本号,或者真正更改名称。该组件应该由其他人使用。该文件应添加到项目中,并在属性中引用..似乎是配置、.ini文件、注册表项的候选。因此,我们的想法是将路径转换为相对路径。。应该可以:)