【问题标题】:Automatically create folders if does not exists in C#如果 C# 中不存在文件夹,则自动创建文件夹
【发布时间】:2017-02-09 17:23:04
【问题描述】:

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

这是我的代码:

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\。

你知道怎么做吗?

【问题讨论】:

  • Directory.CreateDirectory
  • 你遇到了什么错误,设置 OpenOrCreate 标志就足够了。
  • 检查 Directory.Exists 然后创建文件
  • @BalajiMarimuthu 您甚至不需要这样做,您可以直接在现有目录上调用Directory.CreateDirectory,它只会重新运行对现有目录的引用,而不是抛出错误并尝试创建一个新的。
  • @ScottChamberlain 感谢您的解释。

标签: c#


【解决方案1】:

最简单的解决办法是替换

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

与

System.IO.Directory.CreateDirectory(filePath)

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

【讨论】:

  • 请注意,如果找到一个目录,它会抛出一个异常,如 Exception throw: 'System.ComponentModel.Win32Exception' in System.dll
  • 你能显示重新创建它的代码吗,因为文档很清楚:docs.microsoft.com/en-us/dotnet/api/… "返回:一个对象,表示指定路径处的目录。这个对象返回不管指定路径下的目录是否已经存在。"
  • 这是代码 System.IO.Directory.CreateDirectory("Reports");
【解决方案2】:

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

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.
                    }
            }

【讨论】:

  • 1) 您可以在没有问题的目录上调用创建目录。 2) create directory 会自动创建不需要两次调用的子文件夹。
猜你喜欢
  • 2018-02-10
  • 2011-01-19
  • 2018-09-05
  • 2013-01-17
  • 2013-09-18
  • 2011-02-19
  • 1970-01-01
  • 2014-10-19
相关资源
最近更新 更多