473,480 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

@ symbol in C#

SAL
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

Thanks,

Sep 21 '06 #1
32 13738
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon

Sep 21 '06 #2
SAL
Hi Jon,

Yes I meant for Excel to be in there twice. I'm doing testing right and the
directory does exist.

"Jon Skeet [C# MVP]" wrote:
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon

Sep 21 '06 #3
SAL
Hi Jon,

Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I'm testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.

"Jon Skeet [C# MVP]" wrote:
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon

Sep 21 '06 #4
SAL wrote:
Hi Jon,

Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I'm testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.
Notice that the @ symbol is *outside* of the quotation marks and is not
part of the string. In C# the @ symbol means that you don't have to
escape the slashes in a string. For example, the following lines are
equivalent:

string s = "c:\\Program Files\\Blah";

string s = @"c:\Program Files\Blah";

Can you show us the code where you're using the string?

"Jon Skeet [C# MVP]" wrote:
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
>
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
>
The value that strSourceDirectory gets assigned is:
>
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon
Sep 21 '06 #5
Hi SAL,

"SAL" <SA*@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
Please display the string outside of the debugger:
System.Diagnostics.Trace.WriteLine(strSourceDirect ory); or MessageBox.Show
and have a look how the string looks there. You'll see that there is no @
anywhere.

Please look up the path very carefully. Is it completely correct?
What is the API call that gives you the error?
Does the app run in an account which can access that path?

--
SvenC
The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

Thanks,
Sep 21 '06 #6
The @ symbol is for a verbatim string and simply means that escape characters
in the string should be taken litteraly, meaning \ is a \ and not escaping
for a new line like \n. If you notice, the @ sign is not insider the quotes
with the path, there are single quotes around just the path. The @ sign is
put in front to tell you the \'s just mean \. The most likely cause is that
the application doesnt have access to the directories parent or one of its
ancestors. I notice the account whos folder you are looking in is called
admin so it this app is going to need to have file system permissions to look
in admin's profile.
You need to check the account the app runs as and check their permissions to
the directory tree you are looking in.

HTH

Ciaran O'Donnell

"SAL" wrote:
Hi Jon,

Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I'm testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.

"Jon Skeet [C# MVP]" wrote:
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:
>
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
>
The value that strSourceDirectory gets assigned is:
>
@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon
Sep 21 '06 #7
Try another thing:
1. Create folder like this =C:\Test\Documents and Settings\admin\My
Documents\Excel\Excel\
2. Copy that file to new created folder
3. Try your code now for this folder.

If it works then may be you have some permission problems.

"SAL" wrote:
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

Thanks,
Sep 21 '06 #8


SAL wrote:

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
^
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."
So in that error message the path starts with the single quote character
that does not belong into a path.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 21 '06 #9
SAL
Here is the code I am using:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames= Directory.GetDirectories(strSourceDirectory);
string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);

foreach(string strFileName in strarrFileNames)
{
if(Directory.Exists(strSourceDirectory))
{
// This path is a directory
if(File.Exists(strFileName))
{
// This path is a file
ProcessFile(strFileName);
}
else
{
// No File Found
}
}
else
{
// No such directory
}

}// End foreach(string strFileName in strarrFileNames)

I've tried declare the string both ways, with @ and with out and the value
is always the same @"C:\Documents and Settings\admin\My
Documents\Excel\Excel\"

As for the Permissions, I have Admin rights to my box, so I have access to
this directory. Just for simplicity if I declare string
strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the value
@"C:\temp\somedirectory\"
"Chris Dunaway" wrote:
SAL wrote:
Hi Jon,

Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I'm testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.

Notice that the @ symbol is *outside* of the quotation marks and is not
part of the string. In C# the @ symbol means that you don't have to
escape the slashes in a string. For example, the following lines are
equivalent:

string s = "c:\\Program Files\\Blah";

string s = @"c:\Program Files\Blah";

Can you show us the code where you're using the string?

"Jon Skeet [C# MVP]" wrote:
SAL wrote:
I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
>
C# isn't doing that at all. It's just what the debugger is showing you.
The two strings above are the same.
>
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?
>
Then the directory doesn't exist - it's nothing to do with the
backslashes.
>
Did you *mean* to have Excel in there twice, by the way?
>
Jon
>
>

Sep 21 '06 #10

"SAL" <SA*@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
Those are the same string. Look up @ in your C# documentation.
Sep 21 '06 #11
Is this C# codebehind for a web page? Or in your local running application.

Jeff
"SAL" <SA*@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

Thanks,

Sep 21 '06 #12
SAL
This is a Class Library that searches the specified directory I gave for
files to process. A Window Service will at some point excute my Class
Library, but for now I use a Windows App.

"Jeff Dillon" wrote:
Is this C# codebehind for a web page? Or in your local running application.

Jeff
"SAL" <SA*@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

Thanks,


Sep 21 '06 #13
SAL <SA*@discussions.microsoft.comwrote:
Eventually, the directory path will be read from a database table instead of
hard-coding in my program, but for now while I'm testing I have it declared
this way. I did a cut-n-paste from the address line in Windows Explorer to
make sure I had the path correct, but C# is implicitly putting the @ symbol
in front of the path causing errors.
No, that's *not* the problem. It's just not. As others have suggested,
try printing out the string to the console or something similar -
you'll see it's just the debugger's way of showing you the string
without having to escape things.

As I said before, it's not C# itself that's doing anything, it's the
debugger - and it's not the reason for your problem.

What happens when you cut and paste the full file path from the error
message (without the @"') back into Explorer?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #14
SAL wrote:
I've tried declare the string both ways, with @ and with out and the value
is always the same @"C:\Documents and Settings\admin\My
Documents\Excel\Excel\"
I don't think you're getting what everyone here is trying to tell you.
It's pretty clear from the string above that you're reporting to us
what the debugger is telling you (it's coming from a watch, command, or
immediate window).

Forget about finding the file. And don't trust the debugger output; it
often deceives. Trust console output. Try this:

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
Console.WriteLine(strSourceDirectory);
Console.ReadLine();
}
}
}

which prints out:
C:\Documents and Settings\admin\My Documents\Excel\Excel\

Look up the '@' keyword, which lets you specify your directory more
cleanly, like so:
string strSourceDirectory=@"C:\Documents and Settings\admin\My
Documents\Excel\Excel\";

Michael

Sep 21 '06 #15

"SAL" <SA*@discussions.microsoft.comwrote in message
news:A7**********************************@microsof t.com...
| Here is the code I am using:
|
| string strSourceDirectory="C:\\Documents and Settings\\admin\\My
| Documents\\Excel\\Excel\\";
| string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
| string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
|
| foreach(string strFileName in strarrFileNames)
| {
| if(Directory.Exists(strSourceDirectory))
| {
| // This path is a directory
| if(File.Exists(strFileName))
| {
| // This path is a file
| ProcessFile(strFileName);
| }
| else
| {
| // No File Found
| }
| }
| else
| {
| // No such directory
| }
|
| }// End foreach(string strFileName in strarrFileNames)
|
| I've tried declare the string both ways, with @ and with out and the value
| is always the same @"C:\Documents and Settings\admin\My
| Documents\Excel\Excel\"
|
| As for the Permissions, I have Admin rights to my box, so I have access to
| this directory. Just for simplicity if I declare string
| strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
| @"C:\temp\somedirectory\"
|
|

Forget about the @ and tell us which statement throws an exception and what
exception.
Note that you are doing a lot of superfluous checks in your code, if you
call GetFiles, you will get back a list of files in an array
(strarrFileNames), right? Why do you check for the existing of the directory
and the file, if they didn't exist they wouldn't be in the array. That means
you can trim your foreach and keep only:

foreach(string strFileName in strarrFileNames)
{
// This path is a file
ProcessFile(strFileName);
}
Willy.
Sep 21 '06 #16
mp*******@gmail.com wrote:
>
which prints out:
C:\Documents and Settings\admin\My Documents\Excel\Excel\

Look up the '@' keyword, which lets you specify your directory more
cleanly, like so:
string strSourceDirectory=@"C:\Documents and Settings\admin\My
Documents\Excel\Excel\";
In fact, all constant strings that do not require escape characters should use
this modifier.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #17
Hi SAL,

"SAL" <SA*@discussions.microsoft.comwrote in message
news:A7**********************************@microsof t.com...
Here is the code I am using:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);

foreach(string strFileName in strarrFileNames)
{
if(Directory.Exists(strSourceDirectory))
{
// This path is a directory
if(File.Exists(strFileName))
{
// This path is a file
ProcessFile(strFileName);
}
else
{
// No File Found
}
}
else
{
// No such directory
}

}// End foreach(string strFileName in strarrFileNames)
Which line of that code gives you an error? If I replace the ProcessFile
with Console.WriteLine and set strSourceDirectory to a path in my documents
and settings folder I get a list of all the files in there.

Please give us the exact error or exception message. The error is not in the
above code - even if it is a bit unlogical: checking the existence of a
directory in every loop cycle where you have already used that directory
before the loop so there would be an exception before that check.
Mistrusting GetFiles by checking the existence of every file. I think you
should sit down and check the flow of your real app quite carefully to find
the real cause of the error.

--
SvenC

I've tried declare the string both ways, with @ and with out and the value
is always the same @"C:\Documents and Settings\admin\My
Documents\Excel\Excel\"

As for the Permissions, I have Admin rights to my box, so I have access to
this directory. Just for simplicity if I declare string
strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
@"C:\temp\somedirectory\"
"Chris Dunaway" wrote:
>SAL wrote:
Hi Jon,

Eventually, the directory path will be read from a database table
instead of
hard-coding in my program, but for now while I'm testing I have it
declared
this way. I did a cut-n-paste from the address line in Windows
Explorer to
make sure I had the path correct, but C# is implicitly putting the @
symbol
in front of the path causing errors.

Notice that the @ symbol is *outside* of the quotation marks and is not
part of the string. In C# the @ symbol means that you don't have to
escape the slashes in a string. For example, the following lines are
equivalent:

string s = "c:\\Program Files\\Blah";

string s = @"c:\Program Files\Blah";

Can you show us the code where you're using the string?

"Jon Skeet [C# MVP]" wrote:

SAL wrote:
I do not have any trouble declaring string values except when it
comes to
directory path's. Why does C# implicitly put a @ symbol at the
being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

C# isn't doing that at all. It's just what the debugger is showing
you.
The two strings above are the same.

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a
specific
document I want to open does not exist?

Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon


Sep 21 '06 #18

SAL schrieb:
Hello,
Hello

What does happen if you copy the following code into a command line up
and run it?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string myPath = "C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string myNewPath = string.Empty;
string[] myPathParts;

myPathParts = myPath.Split('\\');
for (int i = 0; i < myPathParts.Length; i++)
{
myNewPath += myPathParts[i] + "\\";
System.Console.WriteLine(myNewPath + ": " +
System.IO.Directory.Exists(myNewPath).ToString());
}
System.Console.ReadLine();
}
}
}

Sep 21 '06 #19
Karsten Schramm <Ne**********************@yajirobi.dewrote:
>
SAL schrieb:
>Hello,

Hello

What does happen if you copy the following code into a command line up
and run it?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string myPath = "C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string myNewPath = string.Empty;
string[] myPathParts;

myPathParts = myPath.Split('\\');
for (int i = 0; i < myPathParts.Length; i++)
{
myNewPath += myPathParts[i] + "\\";
System.Console.WriteLine(myNewPath + ": " +
System.IO.Directory.Exists(myNewPath).ToString());
}
System.Console.ReadLine();
}
}
}
What is the point of running this code? Are you trying to determine if .NET
is malfunctioning?

It seems pretty established the the OP does not realize that the exception he
is getting has nothing to do with the @ modifier in front of the string
declaration [or as it appears in the debugger].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #20
Hi,

Thomas T. Veldhouse schrieb:
What is the point of running this code?
To see, if his path is really existing
It seems pretty established the the OP does not realize that the exception he
is getting has nothing to do with the @ modifier in front of the string
declaration [or as it appears in the debugger].
I'm aware of that I because he does not understand we have to show him.

Sep 21 '06 #21
SAL
Hello Everyone,

Sorry, I was in a meeting. I thank everyone for posting your comments here
and I just would like you to know I'm still fairly new to C#.

I have tried the System.Diagnostics.Trace.WriteLine(strSourceDirect ory);
statement and yes the acutual path is C:\Documents and Settings\loescsc\My
Documents\Excel\Excel\ like all of you have been saying. However, that does
not answer my question because my app is still interpeting the @ sign in the
path. Here is the actual Exception error I am getting when I try to open the
file:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

I took the entire path with-out the @ sign and pasted in Windows Explorer
and it opened the file just fine.

Thanks,
"SAL" wrote:
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

Thanks,
Sep 21 '06 #22
SAL, why don't you use a debugger and step through your code. I think you
will find that the problem is in ProcessFile(). For shits and grins replace
the call to ProcessFile(strFileName) with

ProcessFile(@""'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv""")

What are you doing in ProcessFile?

"SAL" <SA*@discussions.microsoft.comwrote in message
news:12**********************************@microsof t.com...
Hello Everyone,

Sorry, I was in a meeting. I thank everyone for posting your comments
here
and I just would like you to know I'm still fairly new to C#.

I have tried the System.Diagnostics.Trace.WriteLine(strSourceDirect ory);
statement and yes the acutual path is C:\Documents and Settings\loescsc\My
Documents\Excel\Excel\ like all of you have been saying. However, that
does
not answer my question because my app is still interpeting the @ sign in
the
path. Here is the actual Exception error I am getting when I try to open
the
file:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

I took the entire path with-out the @ sign and pasted in Windows Explorer
and it opened the file just fine.

Thanks,
"SAL" wrote:
>Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of
a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

Thanks,

Sep 21 '06 #23
SAL <SA*@discussions.microsoft.comwrote:
Sorry, I was in a meeting. I thank everyone for posting your comments here
and I just would like you to know I'm still fairly new to C#.

I have tried the System.Diagnostics.Trace.WriteLine(strSourceDirect ory);
statement and yes the acutual path is C:\Documents and Settings\loescsc\My
Documents\Excel\Excel\ like all of you have been saying. However, that does
not answer my question because my app is still interpeting the @ sign in the
path.
No, it really, really isn't. The @ sign isn't in the path. It's an
artifact of the debugger. That's all.
Here is the actual Exception error I am getting when I try to open the
file:
How are you finding out that error message? Still in the debugger, by
any chance?
@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

I took the entire path with-out the @ sign and pasted in Windows Explorer
and it opened the file just fine.
Then something different is wrong. It's got nothing to do with the @.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #24

SAL wrote:
>
I have tried the System.Diagnostics.Trace.WriteLine(strSourceDirect ory);
statement and yes the acutual path is C:\Documents and Settings\loescsc\My
Documents\Excel\Excel\ like all of you have been saying. However, that does
not answer my question because my app is still interpeting the @ sign in the
path. Here is the actual Exception error I am getting when I try to open the
file:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."
I smell the debugger sneaking back in.

Forget the debugger. Really. It lies to you. Tie it up to a tree for
an hour. Take "Start Debugging" out of your menu list for the
afternoon, map "F5" to "Control-F5", and do this:

try
{
openFileWithPath(path);
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}

I guarantee, with everything in my wallet at the moment, that the '@'
is not in your path string. Look at the console output above, and
you'll see.

Michael

Sep 21 '06 #25
SAL wrote:
Hello,

I do not have any trouble declaring string values except when it comes to
directory path's. Why does C# implicitly put a @ symbol at the being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a specific
document I want to open does not exist?

Here is the acutal error I get:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

Thanks,
The space between "My" and "Documents" needs to be escaped.
Wrap the whole string in double quotes and all will be good
"\"C:\\Documents and Settings\\....\""

Any paths with spaces in them need to be quoted. Win Explorer does this
for you.

JB
Sep 21 '06 #26
John B <jb******@yahoo.comwrote:
The space between "My" and "Documents" needs to be escaped.
Wrap the whole string in double quotes and all will be good
"\"C:\\Documents and Settings\\....\""
That's for the sake of command shells - it doesn't apply to the .NET
API as far as I'm aware.
Any paths with spaces in them need to be quoted. Win Explorer does this
for you.
That's certainly not true for Directory.GetFiles, at least in XP. For
instance, this fails:

using System;
using System.IO;

class Test
{
static void Main()
{
foreach (string file in Directory.GetFiles
("\"c:\\my test dir\""))
{
Console.WriteLine (file);
}
}
}

but this succeeds:

using System;
using System.IO;

class Test
{
static void Main()
{
foreach (string file in Directory.GetFiles (@"c:\my test dir"))
{
Console.WriteLine (file);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #27
Jon Skeet [C# MVP] wrote:
John B <jb******@yahoo.comwrote:
>The space between "My" and "Documents" needs to be escaped.
Wrap the whole string in double quotes and all will be good
"\"C:\\Documents and Settings\\....\""

That's for the sake of command shells - it doesn't apply to the .NET
API as far as I'm aware.
<...>
Doh!
I stand corrected :)
I'll be in the corner kicking the cat.

:)

JB
Sep 21 '06 #28
SAL
Hello,

A new day and fresh mind. Yes, your are right I am doing some unnecessary
checks, but I was only doing that to find out what was the problem. It
didn't dawn on me until this morning that the following code:

string strSourceDirectory="C:\\Documents and Settings\\loescsc\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames= Directory.GetDirectories(strSourceDirectory);

Found the directory path just fine. I tested this by giving
strSourceDirectory a bogus directory and sure enough it failed immediately
just like you said it would. The Exception error it's giving me is
misleading then. Here is the code in my ProcessFile() where I get the
exception error:

try
{

Database db = DatabaseFactory.CreateDatabase("Microsoft.Jet.OLED B.4.0",
strDBSourceDirectory, DBProviderType.OLEDB);

//string sqlCommand = "Select * From [ExtractData]";
string sqlCommand = "Select * From " + strDBSourceDirectory;

DBCommandWrapper dbCommandWrapper =
db.GetSqlStringCommandWrapper(sqlCommand);

StringBuilder readerData = new StringBuilder();

using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
DataTable newDataTable = new DataTable();
newDataTable= CreateDataTable("AP");
// Iterate through DataReader
while (dataReader.Read())
{
DataRow row;
row = newDataTable.NewRow();

// Get the value of the 'Name' column in the DataReader
rowcount++;

row["Invoice_No"] = dataReader["Invoice_No"].ToString();
row["Invoice_Date"] = dataReader["Invoice_Date"].ToString();
row["Equipment_ID"] = dataReader["Equipment_ID"].ToString();
row["Invoice_Line_Item_Amount"] =
dataReader["Invoice_Line_Item_Amount"].ToString();
row["Authorization_No"] = dataReader["Authorization_No"].ToString();

newDataTable.Rows.Add(row);

}//End while (dataReader.Read())

if (dataReader != null) dataReader.Close();

}//End using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
}//End try
catch (Exception ex)
{
// Print error message
Console.WriteLine("Error: {0}",ex.Message.ToString());

}//End catch

This code worked fine in with .xls files, but it should work with .txt and
..csv files as well. To read .xls files I had this line of code
connectionStringData.Parameters.Add(new ParameterData("Extended Properties",
"'Excel 8.0;HDR=Yes;IMEX=1'"));

And I changed it to this for the .txt and .csv files:
connectionStringData.Parameters.Add(new ParameterData("Extended Properties",
"'text;HDR=Yes;FMT=Delimited(,)'"));

I'm using Microsoft Enterprise Library to do my database connections. I
still don't know why it gives this Exception:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure that
the path name is spelled correctly and that you are connected to the server
on which the file resides."

I can take C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv and paste it in Windows Explorer, and
it will open up the file just fine.

"Willy Denoyette [MVP]" wrote:
>
"SAL" <SA*@discussions.microsoft.comwrote in message
news:A7**********************************@microsof t.com...
| Here is the code I am using:
|
| string strSourceDirectory="C:\\Documents and Settings\\admin\\My
| Documents\\Excel\\Excel\\";
| string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
| string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
|
| foreach(string strFileName in strarrFileNames)
| {
| if(Directory.Exists(strSourceDirectory))
| {
| // This path is a directory
| if(File.Exists(strFileName))
| {
| // This path is a file
| ProcessFile(strFileName);
| }
| else
| {
| // No File Found
| }
| }
| else
| {
| // No such directory
| }
|
| }// End foreach(string strFileName in strarrFileNames)
|
| I've tried declare the string both ways, with @ and with out and the value
| is always the same @"C:\Documents and Settings\admin\My
| Documents\Excel\Excel\"
|
| As for the Permissions, I have Admin rights to my box, so I have access to
| this directory. Just for simplicity if I declare string
| strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
| @"C:\temp\somedirectory\"
|
|

Forget about the @ and tell us which statement throws an exception and what
exception.
Note that you are doing a lot of superfluous checks in your code, if you
call GetFiles, you will get back a list of files in an array
(strarrFileNames), right? Why do you check for the existing of the directory
and the file, if they didn't exist they wouldn't be in the array. That means
you can trim your foreach and keep only:

foreach(string strFileName in strarrFileNames)
{
// This path is a file
ProcessFile(strFileName);
}
Willy.
Sep 22 '06 #29
Hi, SAL

which statement actually is throwing the Exception?

It has to be either File.Exists(strFileName)
or ProcessFile(strFileName).
The last is a Method of your class, so it has to be another statement in
there.
"SAL" <SA*@discussions.microsoft.comschrieb im Newsbeitrag
news:A7**********************************@microsof t.com...
Here is the code I am using:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);

foreach(string strFileName in strarrFileNames)
{
if(Directory.Exists(strSourceDirectory))
{
// This path is a directory
if(File.Exists(strFileName))
{
// This path is a file
ProcessFile(strFileName);
}
else
{
// No File Found
}
}
else
{
// No such directory
}

}// End foreach(string strFileName in strarrFileNames)

I've tried declare the string both ways, with @ and with out and the value
is always the same @"C:\Documents and Settings\admin\My
Documents\Excel\Excel\"

As for the Permissions, I have Admin rights to my box, so I have access to
this directory. Just for simplicity if I declare string
strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
@"C:\temp\somedirectory\"
"Chris Dunaway" wrote:
>SAL wrote:
Hi Jon,

Eventually, the directory path will be read from a database table
instead of
hard-coding in my program, but for now while I'm testing I have it
declared
this way. I did a cut-n-paste from the address line in Windows
Explorer to
make sure I had the path correct, but C# is implicitly putting the @
symbol
in front of the path causing errors.

Notice that the @ symbol is *outside* of the quotation marks and is not
part of the string. In C# the @ symbol means that you don't have to
escape the slashes in a string. For example, the following lines are
equivalent:

string s = "c:\\Program Files\\Blah";

string s = @"c:\Program Files\Blah";

Can you show us the code where you're using the string?

"Jon Skeet [C# MVP]" wrote:

SAL wrote:
I do not have any trouble declaring string values except when it
comes to
directory path's. Why does C# implicitly put a @ symbol at the
being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"

C# isn't doing that at all. It's just what the debugger is showing
you.
The two strings above are the same.

How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a
specific
document I want to open does not exist?

Then the directory doesn't exist - it's nothing to do with the
backslashes.

Did you *mean* to have Excel in there twice, by the way?

Jon



Sep 22 '06 #30
SAL
Check my post under Willy. That might help you.

"Christof Nordiek" wrote:
Hi, SAL

which statement actually is throwing the Exception?

It has to be either File.Exists(strFileName)
or ProcessFile(strFileName).
The last is a Method of your class, so it has to be another statement in
there.
"SAL" <SA*@discussions.microsoft.comschrieb im Newsbeitrag
news:A7**********************************@microsof t.com...
Here is the code I am using:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);

foreach(string strFileName in strarrFileNames)
{
if(Directory.Exists(strSourceDirectory))
{
// This path is a directory
if(File.Exists(strFileName))
{
// This path is a file
ProcessFile(strFileName);
}
else
{
// No File Found
}
}
else
{
// No such directory
}

}// End foreach(string strFileName in strarrFileNames)

I've tried declare the string both ways, with @ and with out and the value
is always the same @"C:\Documents and Settings\admin\My
Documents\Excel\Excel\"

As for the Permissions, I have Admin rights to my box, so I have access to
this directory. Just for simplicity if I declare string
strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
@"C:\temp\somedirectory\"
"Chris Dunaway" wrote:
SAL wrote:
Hi Jon,

Eventually, the directory path will be read from a database table
instead of
hard-coding in my program, but for now while I'm testing I have it
declared
this way. I did a cut-n-paste from the address line in Windows
Explorer to
make sure I had the path correct, but C# is implicitly putting the @
symbol
in front of the path causing errors.


Notice that the @ symbol is *outside* of the quotation marks and is not
part of the string. In C# the @ symbol means that you don't have to
escape the slashes in a string. For example, the following lines are
equivalent:

string s = "c:\\Program Files\\Blah";

string s = @"c:\Program Files\Blah";

Can you show us the code where you're using the string?


"Jon Skeet [C# MVP]" wrote:

SAL wrote:
I do not have any trouble declaring string values except when it
comes to
directory path's. Why does C# implicitly put a @ symbol at the
being of a
String value that has been declared like this:

string strSourceDirectory="C:\\Documents and Settings\\admin\\My
Documents\\Excel\\Excel\\";

The value that strSourceDirectory gets assigned is:

@"C:\Documents and Settings\admin\My Documents\Excel\Excel\"
>
C# isn't doing that at all. It's just what the debugger is showing
you.
The two strings above are the same.
>
How do I get around this because my program errors out, because the
directory path in strSourceDirectory that I am assigning to a
specific
document I want to open does not exist?
>
Then the directory doesn't exist - it's nothing to do with the
backslashes.
>
Did you *mean* to have Excel in there twice, by the way?
>
Jon
>
>



Sep 22 '06 #31
But what do you pass to your function, how is it declared? you need to pass
a filepath NOT a directory! that is, strDBSourceDirectory must point to a
file.

foreach(string strFileName in strarrFileNames)
{
// This path is a file
ProcessFile(strFileName);
}

PLEASE PLEASE post the whole code, and do yourself and us a fovor, add this
line to the beginning of your ProcessFile() function, like ...

.... ProcessFile(string fileToProcess)
{
Console.WriteLine(fileToProcess);

Willy.
"SAL" <SA*@discussions.microsoft.comwrote in message
news:19**********************************@microsof t.com...
| Hello,
|
| A new day and fresh mind. Yes, your are right I am doing some unnecessary
| checks, but I was only doing that to find out what was the problem. It
| didn't dawn on me until this morning that the following code:
|
| string strSourceDirectory="C:\\Documents and Settings\\loescsc\\My
| Documents\\Excel\\Excel\\";
| string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
|
| Found the directory path just fine. I tested this by giving
| strSourceDirectory a bogus directory and sure enough it failed immediately
| just like you said it would. The Exception error it's giving me is
| misleading then. Here is the code in my ProcessFile() where I get the
| exception error:
|
| try
| {
|
| Database db = DatabaseFactory.CreateDatabase("Microsoft.Jet.OLED B.4.0",
| strDBSourceDirectory, DBProviderType.OLEDB);
|
| //string sqlCommand = "Select * From [ExtractData]";
| string sqlCommand = "Select * From " + strDBSourceDirectory;
|
| DBCommandWrapper dbCommandWrapper =
| db.GetSqlStringCommandWrapper(sqlCommand);
|
| StringBuilder readerData = new StringBuilder();
|
| using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
| {
| DataTable newDataTable = new DataTable();
| newDataTable= CreateDataTable("AP");
|
|
| // Iterate through DataReader
| while (dataReader.Read())
| {
| DataRow row;
| row = newDataTable.NewRow();
|
| // Get the value of the 'Name' column in the DataReader
| rowcount++;
|
| row["Invoice_No"] = dataReader["Invoice_No"].ToString();
| row["Invoice_Date"] = dataReader["Invoice_Date"].ToString();
| row["Equipment_ID"] = dataReader["Equipment_ID"].ToString();
| row["Invoice_Line_Item_Amount"] =
| dataReader["Invoice_Line_Item_Amount"].ToString();
| row["Authorization_No"] = dataReader["Authorization_No"].ToString();
|
| newDataTable.Rows.Add(row);
|
| }//End while (dataReader.Read())
|
| if (dataReader != null) dataReader.Close();
|
| }//End using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
| }//End try
| catch (Exception ex)
| {
| // Print error message
| Console.WriteLine("Error: {0}",ex.Message.ToString());
|
| }//End catch
|
| This code worked fine in with .xls files, but it should work with .txt and
| .csv files as well. To read .xls files I had this line of code
| connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
| "'Excel 8.0;HDR=Yes;IMEX=1'"));
|
| And I changed it to this for the .txt and .csv files:
| connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
| "'text;HDR=Yes;FMT=Delimited(,)'"));
|
| I'm using Microsoft Enterprise Library to do my database connections. I
| still don't know why it gives this Exception:
|
| @"'C:\Documents and Settings\admin\My
| Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
| the path name is spelled correctly and that you are connected to the
server
| on which the file resides."
|
| I can take C:\Documents and Settings\admin\My
| Documents\Excel\Excel\TEST200609050.csv and paste it in Windows Explorer,
and
| it will open up the file just fine.
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| "SAL" <SA*@discussions.microsoft.comwrote in message
| news:A7**********************************@microsof t.com...
| | Here is the code I am using:
| |
| | string strSourceDirectory="C:\\Documents and Settings\\admin\\My
| | Documents\\Excel\\Excel\\";
| | string [] strarrDirectoryNames=
| Directory.GetDirectories(strSourceDirectory);
| | string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
| |
| | foreach(string strFileName in strarrFileNames)
| | {
| | if(Directory.Exists(strSourceDirectory))
| | {
| | // This path is a directory
| | if(File.Exists(strFileName))
| | {
| | // This path is a file
| | ProcessFile(strFileName);
| | }
| | else
| | {
| | // No File Found
| | }
| | }
| | else
| | {
| | // No such directory
| | }
| |
| | }// End foreach(string strFileName in strarrFileNames)
| |
| | I've tried declare the string both ways, with @ and with out and the
value
| | is always the same @"C:\Documents and Settings\admin\My
| | Documents\Excel\Excel\"
| |
| | As for the Permissions, I have Admin rights to my box, so I have
access to
| | this directory. Just for simplicity if I declare string
| | strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
| value
| | @"C:\temp\somedirectory\"
| |
| |
| >
| Forget about the @ and tell us which statement throws an exception and
what
| exception.
| Note that you are doing a lot of superfluous checks in your code, if you
| call GetFiles, you will get back a list of files in an array
| (strarrFileNames), right? Why do you check for the existing of the
directory
| and the file, if they didn't exist they wouldn't be in the array. That
means
| you can trim your foreach and keep only:
| >
| foreach(string strFileName in strarrFileNames)
| {
| // This path is a file
| ProcessFile(strFileName);
| }
| >
| >
| Willy.
| >
| >
| >
Sep 22 '06 #32
i suppose it's the db.ExecuteReader(dbCommandWraper) that throws the
exception.
And the strDBSourceDirectory contains the path the exception is complaining
about, right?

I'd guess the Statement hasn't the right syntax. Mavbe there should be some
delemiters because of
the spaces in the path or something like that

hth

"SAL" <SA*@discussions.microsoft.comschrieb im Newsbeitrag
news:19**********************************@microsof t.com...
Hello,

A new day and fresh mind. Yes, your are right I am doing some unnecessary
checks, but I was only doing that to find out what was the problem. It
didn't dawn on me until this morning that the following code:

string strSourceDirectory="C:\\Documents and Settings\\loescsc\\My
Documents\\Excel\\Excel\\";
string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);

Found the directory path just fine. I tested this by giving
strSourceDirectory a bogus directory and sure enough it failed immediately
just like you said it would. The Exception error it's giving me is
misleading then. Here is the code in my ProcessFile() where I get the
exception error:

try
{

Database db = DatabaseFactory.CreateDatabase("Microsoft.Jet.OLED B.4.0",
strDBSourceDirectory, DBProviderType.OLEDB);

//string sqlCommand = "Select * From [ExtractData]";
string sqlCommand = "Select * From " + strDBSourceDirectory;

DBCommandWrapper dbCommandWrapper =
db.GetSqlStringCommandWrapper(sqlCommand);

StringBuilder readerData = new StringBuilder();

using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
DataTable newDataTable = new DataTable();
newDataTable= CreateDataTable("AP");
// Iterate through DataReader
while (dataReader.Read())
{
DataRow row;
row = newDataTable.NewRow();

// Get the value of the 'Name' column in the DataReader
rowcount++;

row["Invoice_No"] = dataReader["Invoice_No"].ToString();
row["Invoice_Date"] = dataReader["Invoice_Date"].ToString();
row["Equipment_ID"] = dataReader["Equipment_ID"].ToString();
row["Invoice_Line_Item_Amount"] =
dataReader["Invoice_Line_Item_Amount"].ToString();
row["Authorization_No"] = dataReader["Authorization_No"].ToString();

newDataTable.Rows.Add(row);

}//End while (dataReader.Read())

if (dataReader != null) dataReader.Close();

}//End using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
}//End try
catch (Exception ex)
{
// Print error message
Console.WriteLine("Error: {0}",ex.Message.ToString());

}//End catch

This code worked fine in with .xls files, but it should work with .txt and
.csv files as well. To read .xls files I had this line of code
connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
"'Excel 8.0;HDR=Yes;IMEX=1'"));

And I changed it to this for the .txt and .csv files:
connectionStringData.Parameters.Add(new ParameterData("Extended
Properties",
"'text;HDR=Yes;FMT=Delimited(,)'"));

I'm using Microsoft Enterprise Library to do my database connections. I
still don't know why it gives this Exception:

@"'C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv' is not a valid path. Make sure
that
the path name is spelled correctly and that you are connected to the
server
on which the file resides."

I can take C:\Documents and Settings\admin\My
Documents\Excel\Excel\TEST200609050.csv and paste it in Windows Explorer,
and
it will open up the file just fine.

"Willy Denoyette [MVP]" wrote:
>>
"SAL" <SA*@discussions.microsoft.comwrote in message
news:A7**********************************@microso ft.com...
| Here is the code I am using:
|
| string strSourceDirectory="C:\\Documents and Settings\\admin\\My
| Documents\\Excel\\Excel\\";
| string [] strarrDirectoryNames=
Directory.GetDirectories(strSourceDirectory);
| string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
|
| foreach(string strFileName in strarrFileNames)
| {
| if(Directory.Exists(strSourceDirectory))
| {
| // This path is a directory
| if(File.Exists(strFileName))
| {
| // This path is a file
| ProcessFile(strFileName);
| }
| else
| {
| // No File Found
| }
| }
| else
| {
| // No such directory
| }
|
| }// End foreach(string strFileName in strarrFileNames)
|
| I've tried declare the string both ways, with @ and with out and the
value
| is always the same @"C:\Documents and Settings\admin\My
| Documents\Excel\Excel\"
|
| As for the Permissions, I have Admin rights to my box, so I have access
to
| this directory. Just for simplicity if I declare string
| strSourceDirectory="C:\\temp\\somedirectory\\";, it still gives me the
value
| @"C:\temp\somedirectory\"
|
|

Forget about the @ and tell us which statement throws an exception and
what
exception.
Note that you are doing a lot of superfluous checks in your code, if you
call GetFiles, you will get back a list of files in an array
(strarrFileNames), right? Why do you check for the existing of the
directory
and the file, if they didn't exist they wouldn't be in the array. That
means
you can trim your foreach and keep only:

foreach(string strFileName in strarrFileNames)
{
// This path is a file
ProcessFile(strFileName);
}
Willy.

Sep 22 '06 #33

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5842
by: Richard | last post by:
I am setting up HS in 8i, when I go to $ORACLE_HOME/bin and do a ldd hsodbc i get the below error message, I have done a relink to recreate the hsodbc executable but still no luck. I do have a tar...
0
1667
by: John Graat | last post by:
Hi all, I've built the STLport-462 library on AIX-4.3.3 using gcc-3.3.2. No errors during compilation. However, during linking the following error occurs: ld: 0711-317 ERROR: Undefined symbol:...
9
13237
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
1
4605
by: vsp15584 | last post by:
Hii..i use the coding as below :- import java.applet.applet; import java.awt.*; import com.sun.j3d.utils.applet.mainframe; import com.sun.j3d.utils.universe.*; import...
2
5298
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
1
5528
by: yamitmehta | last post by:
When I compile to code using g++arm of VxWorks 5.5 and put it on my board i get the follwing undefined symbols:- Cpool and Csingleton are template classes. CPool has the static member...
6
121845
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could...
2
6637
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
2685
by: Ryan Gaffuri | last post by:
hlink72@hotmail.com (Eric) wrote in message news:<ab8d8b14.0308220550.54fb5f22@posting.google.com>... LNK1120 is a standard C++ error. you using Visual C++? Means your references a class that...
3
3156
by: Sindhu Rani | last post by:
i hav created 3 classes in 3 different files. am gettin an error durin compilation. wat shud i do??? C:\s\source>javac -d ..\classes devtestdrive.java devtestdrive.java:5: cannot resolve symbol...
0
6912
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...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5348
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 project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
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 ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
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...

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.