2

私はC#にかなり慣れていません。

私の質問は、ファイルを開くダイアログの strFileName とは何ですか?

私は現在このコードを持っています:

 string input = string.Empty;

        OpenFileDialog open = new OpenFileDialog();

        open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

        open.InitialDirectory = "C:";

        if (open.ShowDialog() == DialogResult.OK)

            strFileName = open.FileName;

        if (strFileName == String.Empty)

            return; 

strFileName でエラーが発生します。このコードで何をするかについての説明が見つかりません。

この質問が以前に尋ねられた場合は、お詫び申し上げます。

4

4 に答える 4

3

エラーが何であるかを知らなくても、コードを見るだけでは、strFileName が宣言されていないため、コンパイル エラーが発生する可能性があります。

コードを次のように変更できます。

string input = string.Empty;

OpenFileDialog open = new OpenFileDialog();

open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

open.InitialDirectory = "C:";

if (open.ShowDialog() == DialogResult.OK)

   input = open.FileName;

if (input == String.Empty)

   return; 

またはこれ:

string strFileName = string.Empty;

OpenFileDialog open = new OpenFileDialog();

open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

open.InitialDirectory = "C:";

if (open.ShowDialog() == DialogResult.OK)

   strFileName = open.FileName;

if (strFileName == String.Empty)

   return; 
于 2012-11-02T22:27:27.677 に答える
1

宣言する必要はありましたが、エラーは発生しません。あなたは?

    string strFileName = "";  // added this line - it compiles and runs ok

    private void TestFunc()
    {
        string input = string.Empty;

        OpenFileDialog open = new OpenFileDialog();

        open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

        open.InitialDirectory = "C:";

        if (open.ShowDialog() == DialogResult.OK)

            strFileName = open.FileName;

        if (strFileName == String.Empty)

            return;
    }
于 2012-11-02T22:27:01.403 に答える
1

最初に strFileName を宣言する必要があります

string strFileName = string.empty();

それを使用します。

于 2012-11-02T22:27:35.100 に答える
1

変数 strFilename を文字列として宣言する必要があります。

string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
{
    strFileName = open.FileName;
} 
/* you probably don't want this unless it's part of a larger block of code
if (strFileName == String.Empty)
{
    return; 
}
*/

return strFileName;
于 2012-11-02T22:28:01.980 に答える