2016-11-01 12 views
-2

C# 프로그램에서 csv 파일을 열고 읽는 중 여기에 멈추었습니다. 필자는 3D 매트릭스 그래프를 표시하기 위해 ILNumerics 작업을 시작했지만 예외는 "C : \ Users \ asd \ Documents \ Visual Studio 2013 \ Projects \ matrixgraph \ martix \ bin \ Debug \ stats 파일을 찾을 수 없습니다. csv '. "C#에서 csv 파일을 여는 중 오류가 발생했습니다.

도와주세요! 다음은 코드입니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using ILNumerics; 
using ILNumerics.Drawing; 
using ILNumerics.Drawing.Plotting; 



namespace martix 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void ilPanel1_Load(object sender, EventArgs e) 
     { 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      string path = @"C:\Users\asd\Documents\Visual Studio 2013\Projects\matrixgraph\martix\bin\Debug\stats.csv"; 
      StreamReader sr = new StreamReader(File.Open(path, FileMode.Open)); 
      string dataLines = string.Empty; 
      while (sr.Peek() != -1) 
       dataLines += sr.ReadLine().Replace(";", ",") + "\n"; 
      sr.Close(); 
      dataLines = dataLines.Trim('\n'); 

      //Convert the data-string into an ILArray 
      ILArray<int> AN = ILMath.csvread<int>(dataLines); 

      //Create a new scene, which is the base for our graph 
      var scene = new ILScene(); 
      using (ILScope.Enter()) 
      { 
       //Convert all data points to float 
       ILArray<float> A = ILMath.tosingle(AN); 

       //Add a plot to the scene and configure it 
       scene.Add(new ILPlotCube 
       { 
        //Render in 3D 
        TwoDMode = false, 

        //Add 3 axes 
        Axes = 
        { 

         XAxis = 
         { 
          Label = { Text = "Price in $" }, 
          GridMajor = 
          { 
           DashStyle = DashStyle.Dashed, 
           Color = Color.DarkGray, 
           Width = 1 
          } 
         }, 
         YAxis = 
         { 
          Label = { Text = "Rating count" }, 
          GridMajor = 
          { 
           DashStyle = DashStyle.Dashed, 
           Color = Color.DarkGray, 
           Width = 1 
          } 
         }, 
         ZAxis = 
         { 
          Label = { Text = "Download count" } 
         } 
        }, 
        //Add the data points 
        Children = { 
       new ILPoints { 
        Positions = A 
       }, 
      }, 
        //Set start rotation for 3D rendered graph 
        Rotation = Matrix4.Rotation(new Vector3(1, 1, 1), 0.5f) 
       }); 
      } 

      //Add the scene to the ILPanel 
      ilPanel1.Scene = scene; 
     } 
    } 
} 
+0

'stats.csv' 파일이'bin \ debug'에 없습니다. VS에서 프로젝트를 참조하는 경우'Properties -> Copy to Output Directory'가 항상 복사하거나 최신 일 경우 복사 –

+0

글쎄, 나는 이것을 여러 번 만들었지 만 여전히 나에게 오류를 준다. – Mikka

+0

"matrix"대신 디렉토리가 실제로 "martix"입니까? 또한, [응용 프로그램 폴더 경로를 얻는 가장 좋은 방법은] (http://stackoverflow.com/q/6041332/1115360) 도움이 될 수 있습니다. –

답변

0

경로에있는 공백 일 수 있습니다. 신경 쓰지 마세요, 당신은 축 어적 문자열을 사용하고 있습니다.

경로에 액세스 할 수 있고 네트워크로 연결된 경로가 맞지 않았습니까? 파일을 임시로 이동할 수 있습니까? 실제로 그 경로에 액세스 할 수없는 것 같습니다. 또한

당신이 문제를 파악하기 위해 다음을 수행하려고한다 :

System.IO.FileInfo fi = null; 
    try 
    { 
     fi = new System.IO.FileInfo(path); 
    } 
    catch (ArgumentException) {... } 
    catch (System.IO.PathTooLongException) {... } 
    catch (NotSupportedException) {... } 
    if (ReferenceEquals(fi, null)) 
    { 
     ... 
     // file name is not valid 
    } 
    else 
    { 
     ... 
     // file name is valid... May check for existence by calling fi.Exists. 

    } 

편집 : 사용 System.IO.Directory.GetFiles 해당 폴더에있는 파일의 정확한 이름을 나열하기를, 그 수 있습니다 파일 이름이 다르거 나 (stats.csv.csv) 창 탐색기가 확장을 숨 깁니다.

+0

이어야합니다. 디렉토리에서 URL을 복사했습니다. – Mikka

+0

경로를 변경하려했지만 여전히 같은 오류가 발생했습니다. – Mikka

+0

파일을 사용하지 않았습니까? 이 경우 다른 프로세스에서 사용 중이므로 'C : \ bak \ kofo \ A csv.csv'파일에 액세스 할 수 없습니다. " – brakeroo

0

시도하는 동안 해결책을 얻었습니다. 프로그래밍 방식으로 csv 파일을 만들었으며 이번에는 파일을 읽었습니다.

경로 앞에 몇 줄을 추가하고 경로를 수정했습니다.

StringBuilder csv = new StringBuilder(); 
      csv.AppendLine("112,113,222"); 
      string csvpath = @"C:\\stats\xyz.csv"; 
      File.AppendAllText(csvpath,csv.ToString()); 

      string path = @"C:\stats\xyz.csv"; 

그리고 thats it. 어쨌든 도와 주셔서 감사합니다.