C# 无法打开包含C中空格的文件/目录#

C# 无法打开包含C中空格的文件/目录#,c#,C#,我正在创建windows窗体应用程序。 我无法将输出放入文件中。问题是路径名中有空格 我想创建一个文件名并在下面的位置写入文件 C:\Documents and Settings\admin\Desktop\details.txt 请帮忙 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using

我正在创建windows窗体应用程序。 我无法将输出放入文件中。问题是路径名中有空格

我想创建一个文件名并在下面的位置写入文件

C:\Documents and Settings\admin\Desktop\details.txt

请帮忙

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        String val_input_file_name = textBox1.Text;

        MessageBox.Show(val_input_file_name);
        try
        {

            System.IO.StreamWriter file = new System.IO.StreamWriter(val_input_file_name);
            file.WriteLine("Testing done" + val_input_file_name);
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "Text Files|*.txt";

        DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK) // Test result.
        {
            try
            {
                textBox1.Text = openFileDialog1.FileName;
                MessageBox.Show(openFileDialog1.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
}

使用语句将
StreamWriter
对象包装为

using(var file = new System.IO.StreamWriter(val_input_file_name))
{
    file.WriteLine("Testing done" + val_input_file_name);
}

你能详细说明你的代码是如何“不工作”的吗?你在期待什么,到底发生了什么?如果您遇到异常,请发布发生异常的行和异常详细信息。空格不应成为问题。可能是权限?您没有关闭文件。除了关闭流并释放文件,这将做什么?我相信不关闭流是实际问题。这就是内容未写入文件的原因。.好的,因此dispose方法必须调用
flush()
,这将实际写入文件。谢谢,我相信我没有关闭文件…对于这个愚蠢的问题,我表示道歉。