@ symbol in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SAL

    #31
    Re: @ symbol in C#

    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(str FileName)
    or ProcessFile(str FileName).
    The last is a Method of your class, so it has to be another statement in
    there.
    >
    >
    "SAL" <SAL@discussion s.microsoft.com schrieb im Newsbeitrag
    news:A768C5F5-1C5C-43B0-BEC8-3D607A3CB257@mi crosoft.com...
    Here is the code I am using:

    string strSourceDirect ory="C:\\Docume nts and Settings\\admin \\My
    Documents\\Exce l\\Excel\\";
    string [] strarrDirectory Names=
    Directory.GetDi rectories(strSo urceDirectory);
    string [] strarrFileNames = Directory.GetFi les(strSourceDi rectory);

    foreach(string strFileName in strarrFileNames )
    {
    if(Directory.Ex ists(strSourceD irectory))
    {
    // This path is a directory
    if(File.Exists( strFileName))
    {
    // This path is a file
    ProcessFile(str FileName);
    }
    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:\Documen ts 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
    strSourceDirect ory="C:\\temp\\ somedirectory\\ ";, it still gives me the
    value
    @"C:\temp\somed irectory\"


    "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:\\Progra m 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 strSourceDirect ory="C:\\Docume nts and Settings\\admin \\My
    Documents\\Exce l\\Excel\\";

    The value that strSourceDirect ory gets assigned is:

    @"C:\Documen ts 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 strSourceDirect ory 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
    >
    >
    >
    >
    >
    >
    >

    Comment

    • Willy Denoyette [MVP]

      #32
      Re: @ symbol in C#

      But what do you pass to your function, how is it declared? you need to pass
      a filepath NOT a directory! that is, strDBSourceDire ctory must point to a
      file.

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

      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(str ing fileToProcess)
      {
      Console.WriteLi ne(fileToProces s);

      Willy.


      "SAL" <SAL@discussion s.microsoft.com wrote in message
      news:19E7B10F-8562-4664-94BA-69D6FB2D6A0E@mi crosoft.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 strSourceDirect ory="C:\\Docume nts and Settings\\loesc sc\\My
      | Documents\\Exce l\\Excel\\";
      | string [] strarrDirectory Names=
      Directory.GetDi rectories(strSo urceDirectory);
      |
      | Found the directory path just fine. I tested this by giving
      | strSourceDirect ory 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 .OLEDB.4.0",
      | strDBSourceDire ctory, DBProviderType. OLEDB);
      |
      | //string sqlCommand = "Select * From [ExtractData]";
      | string sqlCommand = "Select * From " + strDBSourceDire ctory;
      |
      | DBCommandWrappe r dbCommandWrappe r =
      | db.GetSqlString CommandWrapper( sqlCommand);
      |
      | StringBuilder readerData = new StringBuilder() ;
      |
      | using (IDataReader dataReader = db.ExecuteReade r(dbCommandWrap per))
      | {
      | DataTable newDataTable = new DataTable();
      | newDataTable= CreateDataTable ("AP");
      |
      |
      | // Iterate through DataReader
      | while (dataReader.Rea d())
      | {
      | DataRow row;
      | row = newDataTable.Ne wRow();
      |
      | // Get the value of the 'Name' column in the DataReader
      | rowcount++;
      |
      | row["Invoice_No "] = dataReader["Invoice_No "].ToString();
      | row["Invoice_Da te"] = dataReader["Invoice_Da te"].ToString();
      | row["Equipment_ ID"] = dataReader["Equipment_ ID"].ToString();
      | row["Invoice_Line_I tem_Amount"] =
      | dataReader["Invoice_Line_I tem_Amount"].ToString();
      | row["Authorization_ No"] = dataReader["Authorization_ No"].ToString();
      |
      | newDataTable.Ro ws.Add(row);
      |
      | }//End while (dataReader.Rea d())
      |
      | if (dataReader != null) dataReader.Clos e();
      |
      | }//End using (IDataReader dataReader = db.ExecuteReade r(dbCommandWrap per))
      | }//End try
      | catch (Exception ex)
      | {
      | // Print error message
      | Console.WriteLi ne("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
      | connectionStrin gData.Parameter s.Add(new ParameterData(" Extended
      Properties",
      | "'Excel 8.0;HDR=Yes;IME X=1'"));
      |
      | And I changed it to this for the .txt and .csv files:
      | connectionStrin gData.Parameter s.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:\Docume nts and Settings\admin\ My
      | Documents\Excel \Excel\TEST2006 09050.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\TEST2006 09050.csv and paste it in Windows Explorer,
      and
      | it will open up the file just fine.
      |
      | "Willy Denoyette [MVP]" wrote:
      |
      | >
      | "SAL" <SAL@discussion s.microsoft.com wrote in message
      | news:A768C5F5-1C5C-43B0-BEC8-3D607A3CB257@mi crosoft.com...
      | | Here is the code I am using:
      | |
      | | string strSourceDirect ory="C:\\Docume nts and Settings\\admin \\My
      | | Documents\\Exce l\\Excel\\";
      | | string [] strarrDirectory Names=
      | Directory.GetDi rectories(strSo urceDirectory);
      | | string [] strarrFileNames = Directory.GetFi les(strSourceDi rectory);
      | |
      | | foreach(string strFileName in strarrFileNames )
      | | {
      | | if(Directory.Ex ists(strSourceD irectory))
      | | {
      | | // This path is a directory
      | | if(File.Exists( strFileName))
      | | {
      | | // This path is a file
      | | ProcessFile(str FileName);
      | | }
      | | 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:\Documen ts 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
      | | strSourceDirect ory="C:\\temp\\ somedirectory\\ ";, it still gives me the
      | value
      | | @"C:\temp\somed irectory\"
      | |
      | |
      | >
      | 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
      | (strarrFileName s), 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(str FileName);
      | }
      | >
      | >
      | Willy.
      | >
      | >
      | >


      Comment

      • Christof Nordiek

        #33
        Re: @ symbol in C#

        i suppose it's the db.ExecuteReade r(dbCommandWrap er) that throws the
        exception.
        And the strDBSourceDire ctory 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" <SAL@discussion s.microsoft.com schrieb im Newsbeitrag
        news:19E7B10F-8562-4664-94BA-69D6FB2D6A0E@mi crosoft.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 strSourceDirect ory="C:\\Docume nts and Settings\\loesc sc\\My
        Documents\\Exce l\\Excel\\";
        string [] strarrDirectory Names=
        Directory.GetDi rectories(strSo urceDirectory);
        >
        Found the directory path just fine. I tested this by giving
        strSourceDirect ory 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 .OLEDB.4.0",
        strDBSourceDire ctory, DBProviderType. OLEDB);
        >
        //string sqlCommand = "Select * From [ExtractData]";
        string sqlCommand = "Select * From " + strDBSourceDire ctory;
        >
        DBCommandWrappe r dbCommandWrappe r =
        db.GetSqlString CommandWrapper( sqlCommand);
        >
        StringBuilder readerData = new StringBuilder() ;
        >
        using (IDataReader dataReader = db.ExecuteReade r(dbCommandWrap per))
        {
        DataTable newDataTable = new DataTable();
        newDataTable= CreateDataTable ("AP");
        >
        >
        // Iterate through DataReader
        while (dataReader.Rea d())
        {
        DataRow row;
        row = newDataTable.Ne wRow();
        >
        // Get the value of the 'Name' column in the DataReader
        rowcount++;
        >
        row["Invoice_No "] = dataReader["Invoice_No "].ToString();
        row["Invoice_Da te"] = dataReader["Invoice_Da te"].ToString();
        row["Equipment_ ID"] = dataReader["Equipment_ ID"].ToString();
        row["Invoice_Line_I tem_Amount"] =
        dataReader["Invoice_Line_I tem_Amount"].ToString();
        row["Authorization_ No"] = dataReader["Authorization_ No"].ToString();
        >
        newDataTable.Ro ws.Add(row);
        >
        }//End while (dataReader.Rea d())
        >
        if (dataReader != null) dataReader.Clos e();
        >
        }//End using (IDataReader dataReader = db.ExecuteReade r(dbCommandWrap per))
        }//End try
        catch (Exception ex)
        {
        // Print error message
        Console.WriteLi ne("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
        connectionStrin gData.Parameter s.Add(new ParameterData(" Extended
        Properties",
        "'Excel 8.0;HDR=Yes;IME X=1'"));
        >
        And I changed it to this for the .txt and .csv files:
        connectionStrin gData.Parameter s.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:\Docume nts and Settings\admin\ My
        Documents\Excel \Excel\TEST2006 09050.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\TEST2006 09050.csv and paste it in Windows Explorer,
        and
        it will open up the file just fine.
        >
        "Willy Denoyette [MVP]" wrote:
        >
        >>
        >"SAL" <SAL@discussion s.microsoft.com wrote in message
        >news:A768C5F 5-1C5C-43B0-BEC8-3D607A3CB257@mi crosoft.com...
        >| Here is the code I am using:
        >|
        >| string strSourceDirect ory="C:\\Docume nts and Settings\\admin \\My
        >| Documents\\Exce l\\Excel\\";
        >| string [] strarrDirectory Names=
        >Directory.GetD irectories(strS ourceDirectory) ;
        >| string [] strarrFileNames = Directory.GetFi les(strSourceDi rectory);
        >|
        >| foreach(string strFileName in strarrFileNames )
        >| {
        >| if(Directory.Ex ists(strSourceD irectory))
        >| {
        >| // This path is a directory
        >| if(File.Exists( strFileName))
        >| {
        >| // This path is a file
        >| ProcessFile(str FileName);
        >| }
        >| 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:\Documen ts 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
        >| strSourceDirect ory="C:\\temp\\ somedirectory\\ ";, it still gives me the
        >value
        >| @"C:\temp\somed irectory\"
        >|
        >|
        >>
        >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
        >(strarrFileNam es), 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(str FileName);
        > }
        >>
        >>
        >Willy.
        >>
        >>
        >>

        Comment

        Working...