2016-11-01 80 views
-2

我在此處打開並閱讀c#程序中的csv文件時出現此問題。我剛開始研究ILNumerics以顯示3D矩陣圖,但異常隨着 「找不到文件」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中的項目,請確保在屬性 - >複製到輸出目錄下是總是複製或複製如果更新 –

+0

嗯,我已經確保了很多次,但它仍然給我錯誤。 – Mikka

+0

目錄是否真的是「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

我從目錄 – Mikka

+0

複製url我甚至試圖改變路徑,但仍然是相同的錯誤。 – 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"; 

而那就是它。無論如何感謝您的幫助:)