C# Streamreader读取同一目录上的文件
我想读取与我的代码位于同一目录下的C# Streamreader读取同一目录上的文件,c#,C#,我想读取与我的代码位于同一目录下的CSV文件 我想阅读.csv public static void InitializeItems() { itemList = new Dictionary<int, Item>(); string filePath = Path.Combine(Directory.GetCurrentDirectory(), "\\Items.csv"); using (StreamReade
CSV
文件
我想阅读.csv
public static void InitializeItems()
{
itemList = new Dictionary<int, Item>();
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "\\Items.csv");
using (StreamReader reader = new StreamReader(filePath))
{
int lineCounter = 0; // Do I really need such a counter for the current line?
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] values = line.Split(',');
string name = values[0];
itemList.Add(lineCounter, new Item(name));
lineCounter++;
}
}
}
private static Dictionary<int, Item> itemList;
publicstaticvoidinitializeItems()
{
itemList=新字典();
字符串filePath=Path.Combine(Directory.GetCurrentDirectory(),“\\Items.csv”);
使用(StreamReader=新StreamReader(文件路径))
{
int lineCounter=0;//我真的需要为当前行设置这样的计数器吗?
而(!reader.EndOfStream)
{
字符串行=reader.ReadLine();
string[]value=line.Split(',');
字符串名称=值[0];
itemList.Add(行计数器,新项目(名称));
lineCounter++;
}
}
}
私有静态字典项列表;
通过这样做,我得到一个System.IO.FileNotFoundException
异常。文件C:\Items.csv
不存在
Directory.GetCurrentDirectory()
返回.exe
文件的路径
路径有什么问题?获取.exe文件路径的方法如下:
AppDomain.CurrentDomain.BaseDirectory
然后,必须将BuildAction
作为Content
放置。这样,文件在生成后被复制到exe文件夹
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file.csv");
获取.exe文件路径的方法如下:
AppDomain.CurrentDomain.BaseDirectory
然后,必须将BuildAction
作为Content
放置。这样,文件在生成后被复制到exe文件夹
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file.csv");
当前目录不是执行exe的目录所必需的:
// Current directory can be whatever you want:
Environment.CurrentDirectory = @"c:\SomeDirectory";
如果您正在查找exe路径,可以尝试
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
就你而言:
using System.Reflection;
...
string filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Items.csv");
编辑:借助Linq:
当前目录不是执行exe的目录所必需的:
// Current directory can be whatever you want:
Environment.CurrentDirectory = @"c:\SomeDirectory";
如果您正在查找exe路径,可以尝试
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
就你而言:
using System.Reflection;
...
string filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Items.csv");
编辑:借助Linq:
要回答您的直接问题,
出了什么问题
:在使用路径时,您不能让文件以反斜杠开头。合并
。你应该这样写:
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "Items.csv");
编辑:
默认情况下,“CurrentDirectory”指向您的exe文件,除非您在代码或快捷方式中对其进行更改。要回答您的直接问题,
出了什么问题
:在使用路径时,您不能让文件以反斜杠开头。合并。你应该这样写:
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "Items.csv");
编辑:
默认情况下,“CurrentDirectory”指向您的exe文件,除非您在代码或快捷方式中更改它。首先建议在项目中创建一个新文件夹来存储文件,并将其命名为MyFiles
,
然后假设您的文件是一个csv
文件,名为test.csv
因此,可以这样访问:
string testFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"MyFiles\test.csv");
首先,建议在项目中创建一个新文件夹来存储文件,并将其命名为MyFiles
,
然后假设您的文件是一个csv
文件,名为test.csv
因此,可以这样访问:
string testFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"MyFiles\test.csv");
Directory.GetCurrentDirectory()返回.exe文件的路径
你确定吗?也许您应该使用AppDomain.CurrentDomain.BaseDirectory
其中任何一个都可以工作,System.IO.Path.GetDirectoryName(Assembly.getExecutionGassembly().Location
System.AppDomain.CurrentDomain.BaseDirectory
System.Environment.CurrentDirectory
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
在Visual Studio中更新时,您是否忘记将文件复制到输出目录?否则,此文件将不会出现在输出目录(可执行文件所在的目录)中,它将只出现在您的项目目录中。目录.GetCurrentDirectory()返回.exe文件的路径
你确定吗?也许你应该使用AppDomain.CurrentDomain.BaseDirectory
其中任何一个都可以,System.IO.path.GetDirectoryName(Assembly.GetExecutionGassembly().Location
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
在Visual Studio中更新时,您是否忘记将文件复制到输出目录?否则,此文件将不会出现在输出目录中(可执行文件所在的位置),它将只在您的项目目录中。因此,我必须将该文件放入调试文件夹?您的代码将我引导到该文件夹,调试时我无法读取该文件。您可以在这里看到问题@mhcumutech:exe文件夹中的Items.csv
?确切位置(即执行文件所在的位置)?如果是,那么它是…\Debug
文件夹,如果不是,请指定位置。我想我会把它放在Debug文件夹中。因为我真的不知道,所以我必须把文件放在Debug文件夹中。你的代码将我带到这个文件夹,我在调试时无法读取文件。你可以在这里看到问题@mhcumputech:wh是否确实要将Items.csv
?放在exe文件夹中(即执行文件的位置)?如果是,则它是。\Debug
文件夹,如果不是,请指定位置。我想我会将它放在Debug文件夹中。因为我还不知道它