代码是n't从函数返回字符串

本文关键字:函数 返回 字符串 代码 | 更新日期: 2023-09-27 18:14:27

我有以下问题:我想检查是否gameName包含函数内部可用的预定义字符串,但不知怎的他没有将它们传输回来。我真的不知道为什么,因为它们是在函数中声明的,应该被传送回主要部分。顺便说一下,OpenFileDialog1的输入是一个名为:h_1 -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;
                }                    
        }

    }

}

}

代码是n't从函数返回字符串

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

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

作为旁注,您可以在这里进行许多优化。一个是改变你的if s和return s看起来像:

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

快乐编码