2017-02-09 9 views
0

特定のパスでファイルを作成しようとしていますが、コードではフォルダを作成できません。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\を作成したいです。

どうすればいいですか?

+4

Directory.CreateDirectory – Gusman

+0

あなたは十分なはずです 'OpenOrCreate'フラグを設定し、どのようなエラーを取得しています。 – Luiso

+0

check directory.Exists次にファイル –

答えて

2

simpelest解決策は、それが存在しないか、それがない場合は何もしないしない場合はディレクトリを作成します

System.IO.Directory.CreateDirectory(filePath) 

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

置き換えるです。

0

最初のフォルダと2番目のディレクトリの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

で解決できました。1)問題なく存在するディレクトリにcreate directoryを呼び出すことができます。 2)ディレクトリを作成すると、2つの呼び出しを必要としないサブフォルダが自動的に作成されます。 –

関連する問題