如果C#中不存在,则自动创建文件夹

时间:2017-02-09 17:23:05

标签: c#

所以,我试图在特定路径创建一个文件,但我所拥有的代码不允许我创建文件夹。

这是我的代码:

public void LogFiles()
{
    string data = string.Format("LogCarga-{0:yyyy-MM-dd_hh-mm-ss}.txt", DateTime.Now);
    for (int linhas = 0; linhas < dataGridView1.Rows.Count; linhas++)
    {
        if (dataGridView1.Rows[linhas].Cells[8].Value.ToString().Trim() != "M")
        {
            var pathWithEnv = @"%USERPROFILE%\AppData\Local\Cargas - Amostras\_logs\";
            var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv);
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                using (StreamWriter writer = File.AppendText(filePath + data))
                {
                    string carga = dataGridView1.Rows[linhas].Cells[0].Value.ToString();
                    string referencia = dataGridView1.Rows[linhas].Cells[1].Value.ToString();
                    string quantidade = dataGridView1.Rows[linhas].Cells[2].Value.ToString();
                    string dataemissao = dataGridView1.Rows[linhas].Cells[3].Value.ToString();
                    string linha = dataGridView1.Rows[linhas].Cells[4].Value.ToString();
                    string marca = dataGridView1.Rows[linhas].Cells[5].Value.ToString().Trim();
                    string descricaoweb = dataGridView1.Rows[linhas].Cells[6].Value.ToString().Trim();
                    string codprod = dataGridView1.Rows[linhas].Cells[7].Value.ToString().Trim();
                    string tipoemb = dataGridView1.Rows[linhas].Cells[8].Value.ToString().Trim();
                    string nomepc = System.Environment.MachineName;
                    writer.WriteLine(carga + ", " + referencia + ", " + quantidade + ", " + dataemissao + ", " + linha + ", " + marca + ", " + descricaoweb + ", " + codprod + ", "
                            + tipoemb + ", " + nomepc);
                    }  
                }  
            }
        }
    }

通用路径中的%USERPROFILE%\AppData\Local\,我想自动创建\Cargas - Amostras\_logs\

你知道怎么做吗?

2 个答案:

答案 0 :(得分:5)

最简单的解决方案是替换

using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))

System.IO.Directory.CreateDirectory(filePath)

如果该目录不存在,那将创建该目录,如果存在则不执行任何操作。

答案 1 :(得分:0)

您需要为第一个文件夹和第二个目录创建两个检查。

var pathWithEnv = @"%USERPROFILE%\AppData\Local\Cargas - Amostras\";
            if (System.IO.Directory.Exists(pathWithEnv))
            {
                pathWithEnv = System.IO.Path.Combine(pathWithEnv, @"_logs\");
                if (System.IO.Directory.Exists(pathWithEnv))
                {
                //Do what you want to do, both directories are found.    
                }
                else
                {
                    System.IO.Directory.CreateDirectory(pathWithEnv);
                    //Do what you want to do, both directories are available.
                }
            }
            else
            {
                System.IO.Directory.CreateDirectory(pathWithEnv);

                    pathWithEnv = System.IO.Path.Combine(pathWithEnv, @"_logs\");
                    if (System.IO.Directory.Exists(pathWithEnv))
                    {
                        //Do what you want to do, both directories are available now.
                    }
                    else
                    {
                        System.IO.Directory.CreateDirectory(pathWithEnv);
                        //Do what you want to do, both directories are created.
                    }
            }