2016-04-09 15 views
1

私は実際にコーディングしたことがありません。それは勉強したことがなく、似たようなもので、自分で学習したことはありませんでした。 しかし、私は2日間問題を抱えています。理解できない部分がありますので、私が助けてくれることを願っています。メソッドから文字列を取得できません

youtubedlCurrentWorker_Process()が作成される前に、私は 'public string CurrentYouTubeDLVersion'を定義しました。
アプリケーションのボタンでyoutubedlCompareVersion_Process()を実行すると、比較ポイントになるとCurrentYouTubeDLVersion文字列が空になります。
以下は私のコードの一部です。

GetCurrentVersionがその前に実行されていたのに、なぜCurrentVideoでCurrentYouTubeDLVersion文字列が空であるのですか?
Visual Studioで "CurrentYouTubeDLVersion"をダブルクリックしても、GetCurrentVersion_Processのリンクにはリンクが表示されません。 youtubedlGetCurrentVersion_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; 
     } 
    } 
} 
+2

を動作するはずですし、それがすべてでは全く接続されていません' public CurrentYouTubeDLVersion'をクラスの先頭に追加しました。 –

+0

グラントウィニーが正しいです。メソッドで 'LatestYoutubeDLVersion'と' CurrentYouTubeDLVersion'の前に 'string'を削除してください。 –

答えて

0

、新しいCurrentYouTubeDLVersion文字列を作成している、そしてそれはあなたがクラスの先頭に追加し、公開CurrentYouTubeDLVersionから完全に独立したのです。代わりに新しいstringを作成するあなたが作ったクラスレベルの変数に

string CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd(); 

割り当て:

CurrentYouTubeDLVersion = youtubedl.StandardOutput.ReadToEnd(); 

その後、値がyoutubedlCompareVersion_Processであなたに利用できるようになります。

+0

ありがとう!これは私の問題を解決しました:) – Dyon

+0

@Dyonようこそ。 –

0

CurrentYouTubeDLVersionの前で「文字列」を取り出し、あなたがメソッド内に新しい `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 */ 
    } 
関連する問題