2017-02-09 3 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

디렉토리를 확인하십시오 .Exists 다음 파일 –

답변

2

simpelest 솔루션은 존재하거나 않는 경우 아무것도하지 않는 경우 디렉토리를 생성합니다

System.IO.Directory.CreateDirectory(filePath) 

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

교체입니다.

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

으로 해결할 수있었습니다. 1) 문제없이 존재하는 디렉토리에서 create directory를 호출 할 수 있습니다. 2) 디렉토리를 만들면 두 번 호출하지 않아도되는 하위 폴더가 자동으로 만들어집니다. –