代码不从函数传回字符串

时间:2011-11-14 17:55:44

标签: c# function

我有以下问题:我想检查gameName是否包含函数内部可用的预定义字符串,但不知何故他没有将它们传回。我真的不知道为什么,因为它们在功能中被声明并且应该被运回主要部分。顺便说一句,OpenFileDialog1的输入是一个名为的文件:hl1-sp-the-infinite-shift.7z

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.IO;

    namespace MapTap2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    }
    public string CheckGame(string sourceName, string gamename)
    { 
        string hl1 = "hl1";
        string hl2 = "hl2";
        string hl2ep2 = "hl2-ep2";
        string hl2ep1 = "hl2-ep1";
        string bs = "bs";
        string prtl = "prtl";
        string prtl2 = "prtl";
        string op = "op";
        bool gamebool;



        gamebool = sourceName.Contains(hl1);
        if (gamebool == true)
        {
            gamename = "hl1";
            return gamename;
        }
            gamebool = sourceName.Contains(hl2);
        if (gamebool == true)
        {
            gamename = "hl2";
            return gamename;
        }
        gamebool = sourceName.Contains(hl2ep2);
        if (gamebool == true)
        {
            gamename = "hl2-ep2";
            return gamename;
        }
        gamebool = sourceName.Contains(hl2ep1);
        if (gamebool == true)
        {
            gamename = "hl2-ep1";
            return gamename;
        }

        gamebool = sourceName.Contains(bs);
        if (gamebool == true)
        {
            gamename = "bs";
            return gamename;
        }
        gamebool = sourceName.Contains(prtl);
        if (gamebool == true)
        {
            gamename = "prtl";
            return gamename;
        }
        gamebool = sourceName.Contains(prtl2);
        if (gamebool == true)
        {
            gamename = "prtl2";
            return gamename;
        }
        gamebool = sourceName.Contains(op);
        if (gamebool == true)
        {
            gamename = "op";
            return gamename;
        }
        else
        {
            gamename = "You sure that this file is from PP?";
            return gamename;
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        openFileDialog1.InitialDirectory = @"C:\Users\";
        openFileDialog1.Filter = "7zip archives|*.7z|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;
    }

    private void cmdCompress_Click(object sender, EventArgs e)
    {
        string sourceName = "";
        string targetFolderName = "testy";


        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        if(openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = openFileDialog1.OpenFile()) != null )
            {
                sourceName = openFileDialog1.FileName;
                lblconsole.Text = sourceName;


                ProcessStartInfo p = new ProcessStartInfo();
                p.FileName = "7za.exe";

                p.Arguments = " e " + sourceName + " -o" + targetFolderName;

                Process x = Process.Start(p);
                x.WaitForExit(); 

                //Check the Game Version of the archive (line 20)
                string gamename = "If you see this in Debug you failed.\n Y U NO MAKE WORKING CODE?";
                CheckGame(sourceName, gamename);
                lblconsole.Text = gamename;
                }                    

        }



    }


}

}

1 个答案:

答案 0 :(得分:1)

调用该函数时,实际上并未将返回值设置为任何值。尝试做

string gamename = "If you see this in Debug you failed.\n Y U NO MAKE WORKING CODE?";
gamename = CheckGame(sourceName, gamename);
lblconsole.Text = gamename;

作为旁注,您可以在此处进行大量优化。一个是将ifreturn更改为:

    if (sourceName.Contains(hl1))
        return = hl1;

快乐编码