-2

I'm stuck here while opening and reading csv file in c# program. Ive just started working upon ILNumerics to display 3D matrix graph, but the Exception rises with "Could not find file 'C:\Users\asd\Documents\Visual Studio 2013\Projects\matrixgraph\martix\bin\Debug\stats.csv'."

Please help me out! Below is the code.

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;
        }
    }
}
Mikka
  • 55
  • 1
  • 7
  • the `stats.csv` file is not in your `bin\debug`.. If it is referenced to your project in VS make sure that under `Properties -> Copy To Output Directory` is Copy Always or Copy If Newer – Gilad Green Nov 01 '16 at 18:15
  • Well i have made sure many times this, still it gives me error. – Mikka Nov 01 '16 at 18:16
  • Is the directory really "martix" instead of "matrix"? Also, [Best way to get application folder path](http://stackoverflow.com/q/6041332/1115360) may help. – Andrew Morton Nov 01 '16 at 18:19
  • Yeah the project name is martix though it had to be matrix – Mikka Nov 01 '16 at 18:24

2 Answers2

0

It may be the spaces you have in the path. Nevermind, you're using verbatim string.

Are you sure that path is accessible and is not a networked mapped path? Can you move your file temporarily? It really seems that you don't have access to that path.

Also you should try doing the following to pinpoint the issue:

    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.

    }

EDIT: use System.IO.Directory.GetFiles to list the exact names of the files in that folder, it may be that the file name is different (stats.csv.csv) and window explorer is hiding the extension.

brakeroo
  • 1,407
  • 13
  • 24
  • i copied the url from the directory – Mikka Nov 01 '16 at 18:25
  • I even tried to change the path, but still the same error. – Mikka Nov 01 '16 at 18:46
  • Are you sure the file is not used? In this case you'll get"The process cannot access the file 'C:\bak\kofo\A csv.csv' because it is being used by another process" – brakeroo Nov 01 '16 at 18:51
  • Also maybe the operating system is reading the file displayed in explorer as "stats.csv" as "stats.csv.csv"...? use System.IO.Directory.GetFiles to list the exact names of the files in that folder.. – brakeroo Nov 01 '16 at 18:55
0

Got the solution while trying. I created the csv file programatically and this time it read the file.

Just added the few line before the path and modified the path.

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";

And thats it. Anyways Thanks for helping :)

Mikka
  • 55
  • 1
  • 7