Escape chracter error?

  • Thread starter Thread starter Dakkar
  • Start date Start date
D

Dakkar

I have an illegal character error so i used @ escape character but it
doesnt work how can i make this work possible

String dir =
myproc.MainModule.FileName.ToString();
FileVersionInfo info =
FileVersionInfo.GetVersionInfo(@dir);

Thanks
 
Hi,

Can you post a sample application that exibits this behavoir? From your
code I cannot tell what is being populated in the dir variable so I am unable
to diagnose properly. Please reply to this post so the group may help you
better.

Thanks.
 
AFAIK @ for strings only applies to string literals:
string stringOne = "Hello\tWorld";
string stringTwo = @"Hello\tWorld";

if(@stringOne == stringTwo) {
// This will never execute, since stringOne has a tab character where
stringTwo has a literal "\t" in it.
}

The other main use of @ is to allow you to use keywords as names in your
application:
int public = 0; // Compile error
int @public = 0; // No compile error

Dakkar said:
I have an illegal character error so i used @ escape character but it
doesnt work how can i make this work possible

String dir =
myproc.MainModule.FileName.ToString();
FileVersionInfo info =
FileVersionInfo.GetVersionInfo(@dir);

Thanks
 
Dakkar said:
I have an illegal character error so i used @ escape character but it
doesnt work how can i make this work possible

String dir =
myproc.MainModule.FileName.ToString();
FileVersionInfo info =
FileVersionInfo.GetVersionInfo(@dir);


I don't think you understand what "@" is for. It's to either specify
that the following token is an identifier even if it's a keyword (so
you could use "if" as a variable by writing @if everywhere, for
example) or to specify that a string literal should be treated
verbatim. It doesn't change the value of a string variable or anything
like that.

See http://www.pobox.com/~skeet/csharp/strings.html for more
information.
 
So what should i do ?
my string is a process path like C:\Program Files\etc
how can i prevent this illegal character error
 
Here is my complete program


public bool proccescheck()
{
int count=0;

MyCmd = new OdbcCommand();
MyConn = new OdbcConnection("DRIVER={MySQL ODBC
3.51 Driver};" + "SERVER=localhost;" +
"DATABASE=connector;" + "UID=root;" +
"PWD=root;");
MyConn.Open();
MyCmd.Connection = MyConn;
StringBuilder procheckbuild = new StringBuilder();
procheckbuild.Append("select name,memory from
programlar");
MyCmd.CommandText = procheckbuild.ToString();
OdbcDataReader procheck =
MyCmd.ExecuteReader(CommandBehavior.CloseConnection);

while(procheck.Read())
{
count++;
}
procheck.Close();


Process[] myproc = Process.GetProcesses();
for (int i = 0; i < myproc.Length; i++)
{
String procname = myproc.ProcessName;
for (int j = 1; j <= count; j++)
{
MyCmd = new OdbcCommand();
MyConn = new
OdbcConnection("DRIVER={MySQL ODBC 3.51
Driver};" + "SERVER=localhost;" +
"DATABASE=connector;" + "UID=root;" +
"PWD=root;");
MyConn.Open();
MyCmd.Connection = MyConn;
StringBuilder proceskontrol = new
StringBuilder();
proceskontrol.Append("select name,memory FROM
programlar where id ='");
proceskontrol.Append(j);
proceskontrol.Append("'");
MyCmd.CommandText = proceskontrol.ToString();
OdbcDataReader prochecking =
MyCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (prochecking.Read())
{
String program =
prochecking.GetString(0);
String memorysize =
prochecking.GetString(1);
String dir =
@myproc.MainModule.FileName.ToString();
FileVersionInfo myinfo =
FileVersionInfo.GetVersionInfo(dir);
int memory = int.Parse(memorysize);
if
(myproc.ProcessName.Equals(program) ||
myproc.PrivateMemorySize64 == memory)
{
this.txt1.Text +=
myinfo.OriginalFilename.ToString();

}
}
MyConn.Close();
}
}
return true;
}
 
Hi Dakkar,

Thanks for posting your code. It always helps in diagnosis. I think that
you would benefit from creating a simple program and outputting your results
on the command line. Take a look at Jon's post earlier for a link on how to
do this. In the meantime...

In the statement:

String dir = @myproc.MainModule.FileName.ToString();

remove the @ symbol. There is no point in it here. Then add a breakpoint
in your code at this point and examine the value of dir. If this looks
acceptable (e.g. a real path name and file then move on to the
FileVersionInfo myinfo variable. Once this variable has been assigned a
value examine the contents to ensure that you haven't encountered an error.
My bet is that you cannot get properties for the file for some reason or the
file name is not a good one. You probably should wrap this whole bit of code
in a try...catch block to ensure that everything is getting executed
properly.

On a side note...It has been my experience that when you attempt to get a
value that was assigned to the FileVersionInfo there is a high possibility
that an exception can be thrown (i.e. if something else is holding on to the
file and it cannot access it, etc...) so you want to handle these and react
appropriately.
 
Dakkar said:
Here is my complete program


public bool proccescheck()

<snip>

Complete programs don't start by declaring methods. They will always
include a type declaration before a method declaration.
 
Back
Top