473,748 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Could not load the file exception when trying to read a file with StreamReader

21 New Member
Hi,

I am using the following code to read a file.

DirectoryInfo dir = new DirectoryInfo(@ "C:\");
try
{
if (dir.Exists)
{
FileInfo[] csvfiles = dir.GetFiles("* .csv");
foreach (FileInfo file in csvfiles)
{
Console.WriteLi ne(file.Name );
Console.Read();
using (StreamReader sr = new StreamReader(fi le.Name))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.

while ((line = sr.ReadLine()) != null)
{
Console.WriteLi ne(line);

}
}
}

}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}


But, this code is running up to streamreader class and giving error saying that could not load file "C:\DotnetAppli cation\FileOper ations\test.csv " - this is the file path where my application exists. The 'test.csv' file actually presented under C:\ directory.
Please advice how to fix this?

TIA
Jul 8 '09 #1
1 1887
NitinSawant
270 Contributor
When initializing the StreamReader object, you need to pass the full path in the constructor...

Expand|Select|Wrap|Line Numbers
  1. using (StreamReader sr = new StreamReader(dir.FullName+file.Name))
  2.  
Jul 9 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4507
by: Alex | last post by:
Hello, in my company, we have a diagnostic tool for hardware. Depending on the hardware projcet, a different project configuration is needed. The configuration file is written in XML and contains information for the visualization and storage of the retrieved data. From mid july till now, i have made 4 different configuration sets and everything was fine. Today, i got the order to make a new configuration set. When the xml file was...
3
1872
by: Sharon | last post by:
I'm using StreamWriter to write to file and at the same time I'm using the StreamReader to read from the same file. Doing that cause an exception when I'm trying to create the StreamReader when the file is already opened fro writing by the StreamWriter. How can I write and read at the same time to the same file? -- Thanks Sharon G.
9
35312
by: haibhoang | last post by:
I have a Windows Service that is trying to parse a large (> 1Gig) text file. I am keep getting OutOfMemoryException exception. Here is the code that's having problem: using (StreamReader streamReader = new StreamReader(stream, Encoding.ASCII)) { string line = ""; DateTime currentDate = DateTime.Now.Date; while (streamReader.Peek() > -1)
1
6077
by: C.W. | last post by:
I am trying to connect to a remote server in order to perform an http post inside my asp.net page. However, I consistently get the following error: System.Net.Sockets.Socket.Receive(Byte buffer, Int32 offset, Int32 size, SocketFlags socketFlags) System.Net.Sockets.NetworkStream.Read(Byte buffer, Int32 offset, Int32 size)
3
1797
by: kymjay | last post by:
I am trying to read a file in with the following code and I am getting a system.nullreferenceexception error. The error is “object reference not set to an instance of an object”. This is my code below and I can’t figure out what I am doing wrong. I have used this before and know it works….there is no reason for this not too. Help, please Private strPhraseFile As Strin Private intPhrases As Intege Private objReader As StreamReade...
7
5353
by: John Dann | last post by:
I'm tripping over a silly little problem when trying to read a text file, but can't see the fix. Outline code is: ---------------------------------------------------- FS=New FileStream(filename, filemode) Dim SR as New Streamreader(FS) Try Read file Catch ex as exception
5
3514
by: andrewcw | last post by:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to move an application forward to C#, but the samples for MD5 hash using the framework I tried gave different hashes. What do I feed the framework and how do I get the same values ?? The following code DOES NOT GIVE THE FCIV value: public string getFileHash(string filePath) { string retVal = "";
13
11153
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
1
8850
by: Doctorslicer | last post by:
Hi All, We have a large multi project application in MFC that we are converting to managed code. We have been able to add /CLR to all of the projects and have compiled successfully but encounter an exception when loading one of our Extension DLL's. 'OurApp.exe' (Managed): Loaded '...\Debug\DLL1.dll', Symbols loaded. 'OurApp.exe' (Managed): Loaded '...\Debug\DLL2.dll', Symbols loaded. First-chance exception at 0x7c812a5b in OurApp.exe:...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.