@ symbol in C#

G

Guest

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,
 
J

Jon Skeet [C# MVP]

SAL said:
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
 
G

Guest

Hi Jon,

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

Guest

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.
 
C

Chris Dunaway

SAL said:
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?
 
S

SvenC

Hi SAL,

SAL said:
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(strSourceDirectory); 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?
 
G

Guest

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
 
G

Guest

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.
 
M

Martin Honnen

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.
 
G

Guest

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\"
 
M

Michael A. Covington

SAL said:
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.
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

SAL said:
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?
 
M

mpetrotta

SAL said:
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
 
W

Willy Denoyette [MVP]

| 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.
 
T

Thomas T. Veldhouse

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.
 
S

SvenC

Hi SAL,

SAL said:
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.
 
K

Karsten Schramm

SAL said:

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 + "\\";
System.Console.WriteLine(myNewPath + ": " +
System.IO.Directory.Exists(myNewPath).ToString());
}
System.Console.ReadLine();
}
}
}
 
T

Thomas T. Veldhouse

Karsten Schramm said:

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 + "\\";
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].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top