By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

@ symbol in C#

P: n/a
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
Share this Question
Share on Google+
32 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.