【问题标题】:Unable to get a string out of a method无法从方法中获取字符串
【发布时间】:2016-07-30 17:41:26
【问题描述】:

我对编码真的很陌生,从未学习过它或类似的东西,只是自己学习,以前从未做过,但我正在尝试创建我的第一个真正的新应用程序。 但是,我这两天有一些问题,我就是想不通,所以希望你能帮助我。

好的,所以在创建 youtubedlCurrentWorker_Process() 之前,我确实定义了“公共字符串 CurrentYouTubeDLVersion”。
但是,当我的应用程序中的按钮执行 youtubedlCompareVersion_Process() 时,CurrentYouTubeDLVersion 字符串在比较点处为空。
下面只是我的代码的一小部分。

为什么字符串 CurrentYouTubeDLVersion 在 CompareVersion 中为空,而 GetCurrentVersion 在它之前运行?
即使我在 Visual Studio 中双击“CurrentYouTubeDLVersion”,它也不会显示指向 GetCurrentVersion_Process 中的链接。

namespace MediaDownloader
{
public partial class updates : UserControl
    {
        public string LatestYoutubeDLVersion;
        public string CurrentYouTubeDLVersion;

    public void youtubedlGetCurrentVersion_Process()
    {
        if (File.Exists(YouTubeDLPath))
        {
            //Here I get the current version of youtube-dl.exe, to get the version number, we have to run youtube-dl.exe --version
            Process youtubedl = new Process();
            youtubedl.StartInfo.CreateNoWindow = true;
            youtubedl.StartInfo.UseShellExecute = false;
            youtubedl.StartInfo.RedirectStandardOutput = true;
            youtubedl.StartInfo.RedirectStandardError = true;
            youtubedl.StartInfo.FileName = YouTubeDLPath;
            youtubedl.StartInfo.Arguments = " --version";
            youtubedl.Start();
            string CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd();
            this.Dispatcher.Invoke((Action)(() =>
            {
                CurrentYouTubeDLVersionText.Text = "Current youtube-dl.exe version: " + CurrentYouTubeDLVersion;
                YouTubeDLVersionStatusText.Text = null;
                UpdateYouTubeDL.IsEnabled = false;
            }));

        }
    public void youtubedlCompareVersion_Process()
    {
        youtubedlGetCurrentVersion_Process();
        string LatestYoutubeDLVersion = WebClient.DownloadString("https://yt-dl.org/latest/version");
        MessageBox.Show("Latest:" + LatestYoutubeDLVersion + "Current " + CurrentYouTubeDLVersion);
        int YouTubeDLUptodate = CurrentYouTubeDLVersion.CompareTo(LatestYoutubeDLVersion);
        if (YouTubeDLUptodate < 1)
        {
            YouTubeDLVersionStatusText.Text = "Your youtube-dl.exe is out of date, please click the button below to update.";
            UpdateYouTubeDL.IsEnabled = true;
        }
        else
        {
            YouTubeDLVersionStatusText.Text = "youtube-dl.exe is up to date!";
            UpdateYouTubeDL.IsEnabled = false;
        }
    }
}

【问题讨论】:

  • 格兰特温尼是对的。在您的方法中删除 LatestYoutubeDLVersionCurrentYouTubeDLVersion 之前的 string

标签: c# string void


【解决方案1】:

youtubedlGetCurrentVersion_Process 方法中,您正在创建一个新的CurrentYouTubeDLVersion 字符串,它与您添加到类顶部的公共CurrentYouTubeDLVersion 完全分开。

string CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd();

分配给您创建的类级变量,而不是创建新的string

CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd();

然后您可以在youtubedlCompareVersion_Process 中使用该值。

【讨论】:

    【解决方案2】:

    取出 CurrentYouTubeDLVersion 前面的“字符串”,它应该可以工作

        public youtubedlGetCurrentVersion_Process()
        {
           /* removed code to make easier to read */
           //string CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd();
           CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd();
           /* removed code to make easier to read */
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2017-01-05
      • 2019-10-27
      相关资源
      最近更新 更多