Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C文件未保存在正确的文件夹中_C#_File - Fatal编程技术网

C# C文件未保存在正确的文件夹中

C# C文件未保存在正确的文件夹中,c#,file,C#,File,我需要保存一个正确的创建文件夹的txt文件。但是它节省了C:\Nova意大利面我需要节省C:\Nova意大利面\+valor.retrona_nome+comboBox1.Text怎么了 private void btn_SaveFile_Click(object sender, EventArgs e) { objSQL.Search_RGP_CadastroPrint(Convert.ToInt32(comboBox1.Text), str_list); objSQL.Sea

我需要保存一个正确的创建文件夹的txt文件。但是它节省了C:\Nova意大利面我需要节省C:\Nova意大利面\+valor.retrona_nome+comboBox1.Text怎么了

private void btn_SaveFile_Click(object sender, EventArgs e)
{
    objSQL.Search_RGP_CadastroPrint(Convert.ToInt32(comboBox1.Text), str_list);
    objSQL.SearchPrint(Convert.ToInt32(comboBox1.Text));

    string path = @"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;

    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);

    }
    StreamWriter file = new System.IO.StreamWriter(path + ".txt");

    file.WriteLine("---------------------------------------------------------------------------------------------------------");
    file.WriteLine("Nome: " + valor.retorna_nome);
    file.WriteLine("RGP: " + comboBox1.Text);
    file.WriteLine("Endereço: " + valor.retorna_endereco);
    file.WriteLine("Telefone: " + valor.retorna_telefone + " Celular: " + valor.retorna_celular + "\r\n");
    str_list.ForEach(file.WriteLine);

    file.Close();
}
说valor.retrona_nome是hello,comboBox1.Text是world。您的代码执行以下操作:

string path = @"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;
// -> path = "C:\Nova pasta\helloworld"

if (!Directory.Exists(path))
{
    Directory.CreateDirectory(path);
    // -> created directory "C:\Nova pasta\helloworld"
}

StreamWriter file = new System.IO.StreamWriter(path + ".txt");
// -> writes to file "C:\Nova pasta\helloworld.txt"

所以它完全按照你说的去做。您希望该目录被称为什么?文件名?

您的字符串路径类似于:C:\Nova意大利面\aNameXXX

其中:

阿纳姆=勇气

XXX=Combobox1.Text

创建目录时,此操作必须成功,但在此之后,文件路径为: path+.txt:C:\Nova意大利面\aNameXXX.txt

它正在文件夹旁边创建一个名为aNameXXX.txt的文件

您需要在文件中添加\和名称,以创建如下路径:C:\Nova意大利面\aNameXXX\FILENAME.txt

StreamWriter file = new System.IO.StreamWriter(path + "\" + FILENAME + ".txt");

valor.Retrona_nome和comboBox1.Text应该代表什么?它的2个字符串。valor.retrona_nome返回名称,combobox1.text返回字符串编号当您在字符串路径上添加断点=@C:\Nova意大利面\+valor.retrona_nome+Combox1.text;它的价值是什么?也可以考虑使用PATION.COMPITEIS路径,它是一个文件名,还是一个目录名?您似乎将其视为两者。哼哼首先验证文件夹是否存在如果不存在创建一个新文件夹,然后在此新文件夹中创建一个新的txt文件。问题是他没有这样做。如果存在,只需在其中创建文件;如果不存在,则创建文件夹,然后在中创建文件。