提示コードですが下記のForm1.csの上記のFunction.Init();が以下のエラーのようなエラーが出てしまい。実行できません。なぜでしょうか?
staticクラスはインスタンスを生成しないで利用でき、C#の場合は#include のようなものは存在しないのでそのまま別のソースファイルのクラスがそのまま使えると調べたのですが原因がわかりません。
エラー
重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態 エラー IDE1007 名前 'Function.Init' は、現在のコンテキストに存在しません。 YoutubeDownloader C:\Users\yw325\Desktop\YoutubeDownloader\YoutubeDownloader\Form1.cs 15 アクティブ
Function.cs
cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System; using System.IO; namespace YoutubeDownloader { static class Function { private static Process pro = new Process(); /* ########################### 初期化 ###########################*/ static public void Init() { pro.StartInfo.FileName = "youtube-dl"; pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = true; // コンソール・ウィンドウを開かない pro.StartInfo.UseShellExecute = false; // シェル機能を使用しない } /* ########################### URLからデータ取得 ###########################*/ static public Data GetData(String? url) { Data data = new Data(); pro.StartInfo.Arguments = " --get-id " + url; pro.Start(); pro.WaitForExit(); string result = pro.StandardOutput.ReadToEnd(); if (result == "") { MessageBox.Show("URL ERROR", "", MessageBoxButtons.OK, MessageBoxIcon.Error); data.id = null; return data; } else { data.id = result; return data; } } } }
Form1.cs
cs
namespace YoutubeDownloader { using System.Diagnostics; using System; using System.IO; using System.Diagnostics; //public class public partial class Form1 : Form { /////////////////////////////////////////////////////////////// Function.Init(); /////////////////////////////////////////////////////////////// private Process pro = new Process(); //List<Data> dataList = new List<Data>(); public void AddList(String? str) { } String setURLString = "Set URL"; String setStopString = "Stop Download"; String getNowURLString = "Now Loading"; String selectFormatString = "Select Format"; String nowSelectString = "Select SaveFolder"; String nowSaveFolderString = " Now Download"; //FolderBrowserDialog folderBrowser = new FolderBrowserDialog(); bool isDownLoad = false; bool isSetURL = false; String? initFileName = null; String? fileName = null; String? url = null; String? format = null; String? savePath = null; //指定した文字以降を削除 static public string GetRemoveRight(string str, string removeStr) { int length = str.IndexOf(removeStr); if (length < 0) { return str; } return str.Substring(0, length); } //ダウンロード private void DownLoad() { SetFileName(); //ファイル名を設定 fileName = fileName.Replace("\r", "").Replace("\n", ""); //改行を削除 //pro.StartInfo.Arguments = " -o " + "\""+fileName + "\" " + url + " ytsearch:" + format; //pro.Start(); Debug.WriteLine(pro.StartInfo.Arguments); button.Text = setStopString; //ボタンの表示を停止ボタンに変える isDownLoad = true; //ダウンロード中 while (pro.HasExited == false) { String? progress = pro.StandardOutput.ReadLine(); //プログレスバーを表示 if(progress != null) { if (progress.Contains("ETA") == true) { String? p = progress.Replace("[download]", ""); if (p != null) { progress = p; } progress = GetRemoveRight(progress, "%"); progress = progress.Replace(" ", ""); progressBar1.Value = (int)float.Parse(progress); } } } pro.WaitForExit(); //リセット url = null; format = null; textBox_url.Text = null; isDownLoad = false; //ダウンロード終了 button.Text = setURLString; //URLを指定する表示に変更 } public Form1() { InitializeComponent(); } private void SetFileName() { pro.StartInfo.Arguments = " --get-filename " + url; pro.Start(); pro.WaitForExit(); fileName = pro.StandardOutput.ReadToEnd(); //Debug.WriteLine(results); //return results; } /*############################ 初期化 ############################*/ private void Form1_Load(object sender, EventArgs e) { button.Text = setURLString; //URLを指定する表示に変更 } //ダウンロードボタンをクリック private void Button_Click(object sender, EventArgs e) { DownLoad(); } //URL入力欄でEnterキーを押下 private void URLText_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { } } //セーブファイルをボタンをクリック private void PathButton_Click(object sender, EventArgs e) { folderBrowser.ShowDialog(); savePath = folderBrowser.SelectedPath; button.Text = nowSaveFolderString; //Debug.WriteLine(savePath); } //URLを入力をしたとき private void URLText_TextChanged(object sender, EventArgs e) { url = textBox_url.Text; } } }
まだ回答がついていません
会員登録して回答してみよう