C# 如何在OpenFileDialog对话框中为“取消”按钮编码

C# 如何在OpenFileDialog对话框中为“取消”按钮编码,c#,winforms,visual-studio-2010,openfiledialog,dialogresult,C#,Winforms,Visual Studio 2010,Openfiledialog,Dialogresult,每当我取消OpenFile对话框时。它给出的错误路径为空。 我的 有没有办法为OpenFileDialog的取消按钮和关闭按钮编码 我的代码: private void按钮4\u单击(对象发送者,事件参数e) { 字符串s=图像打印()+打印图像(); PrintFactory.sendTextToLPT1(s);/发送到串行端口 } 私有字符串图像_print() { OpenFileDialog ofd=新建OpenFileDialog(); 字符串路径=”; 字符串full_path=“”

每当我取消OpenFile对话框时。它给出的错误路径为空。 我的

有没有办法为OpenFileDialog的取消按钮和关闭按钮编码

我的代码:

private void按钮4\u单击(对象发送者,事件参数e)
{
字符串s=图像打印()+打印图像();
PrintFactory.sendTextToLPT1(s);/发送到串行端口
}
私有字符串图像_print()
{
OpenFileDialog ofd=新建OpenFileDialog();
字符串路径=”;
字符串full_path=“”;
字符串文件名_noext=“”;
ofd.InitialDirectory=@“C:\ZTOOLS\FONTS”;
ofd.Filter=“GRF文件(*.GRF)|*.GRF”;
ofd.FilterIndex=2;
ofd.RestoreDirectory=true;
if(ofd.ShowDialog()==DialogResult.OK)
{
filename\u noext=System.IO.Path.GetFileName(ofd.filename);
path=path.GetFullPath(ofd.FileName);
img_path.Text=文件名\u noext;
//Show(filename_noext,“filename”);
//显示(完整路径,“路径”);
//将文件从位置移动到调试位置
字符串replacepath=@“E:\Debug”;
字符串文件名=System.IO.Path.GetFileName(路径);
字符串newpath=System.IO.Path.Combine(replacepath,fileName);
如果(!System.IO.File.Exists(filename\u noext))
System.IO.File.Copy(路径,newpath);
}
//尝试使用以下代码,但未使用返回语句。表示“需要可转换为'string'类型的对象”
if(ofd.ShowDialog()!=DialogResult.OK)
return;//--->>此处不返回
//每当我按下“取消”或“关闭”按钮时,它都会在下面一行。如何停止此操作
StreamReader test2=新的StreamReader(img_path.Text);
字符串s=test2.ReadToEnd();
返回s;
}        
私有字符串Print_image()
{
//一些以s为单位返回字符串值的代码
返回s;
}
为什么不:

else
语句避免了重复的逻辑,并返回一个空字符串,调用方法可以检查该字符串

if (ofd.ShowDialog() == DialogResult.OK)
{
    filename_noext = System.IO.Path.GetFileName(ofd.FileName);
    path = Path.GetFullPath(ofd.FileName);
    img_path.Text = filename_noext;
    // MessageBox.Show(filename_noext, "Filename");
    // MessageBox.Show(full_path, "path");
    // move file from location to debug
    string replacepath = @"E:\Debug";
    string fileName = System.IO.Path.GetFileName(path);
    string newpath = System.IO.Path.Combine(replacepath, fileName);
    if (!System.IO.File.Exists(filename_noext))
        System.IO.File.Copy(path, newpath);

}
else
{
    return String.Empty;
}
它不工作,因为您应该返回字符串!如果需要,您可以返回null并检查null

在你的方法中:

private void button4_Click(object sender, EventArgs e)
{
    if(image_print() == "")
    {
       return;
       //You can write a message here to tell the user something if you want.
    }

    string s = image_print() + Print_image();
    PrintFactory.sendTextToLPT1(s); / sending to serial port
}

当我给出上述代码时。。现在OpenFileDialog将出现两次。。当我取消OpenFileDialog时,它会再次打开..这是因为。。在按钮4中单击,会出现
图像打印()
。。还有。
string s=image\u print()
。那么该做什么呢?这段代码运行得很好,但现在。。发生的情况意味着,当我按下cancel时,它将进入行
StreamReader test2=new StreamReader(img_path.Text)如果我没有选择任何文件,如何防止这种情况
private void button4_Click(object sender, EventArgs e)
{
    if(image_print() == "")
    {
       return;
       //You can write a message here to tell the user something if you want.
    }

    string s = image_print() + Print_image();
    PrintFactory.sendTextToLPT1(s); / sending to serial port
}