我在c#程序中打开和读取csv文件时被困在这里。我刚刚开始研究ILNumerics来显示3D矩阵图,但是Exception也随之而来 &#34;找不到文件&#39; C:\ Users \ asd \ Documents \ Visual Studio 2013 \ Projects \ matrixgraph \ martix \ bin \ Debug \ stats.csv&#39;。&#34; < /强>
请帮帮我! 以下是代码。
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 :(得分: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),窗口浏览器隐藏了扩展名。
答案 1 :(得分: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";
就是这样。无论如何感谢您的帮助:))