Solved

Moving files in .NET C# invalid characters

Posted on 2010-11-16
3
484 Views
Last Modified: 2012-05-10
I have a loop that compares a filename in my database to a folder of files that should match the filenames.  When I do this line to move the files out of their folder and into a destination directory, it doesn't get all of the files moved.  I suspect it's because of some invalid characters.  I replaced the single quote.  What others should I be looking for?  And/or what's the best way to account for any filename.  

Do I need to check for backslashed too?  Or does the @ take care of that?

strSourceFile = row["Filename"].ToString().Replace("'", "''");
File.Move(@"C:\SHAT_TRACKS\" + strSourceFile, strDestPath + @"\" + strSourceFile);
0
Comment
Question by:JT_SIRO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 34147452
Apostrophe isn't an invalid character in file names. Are you sure you aren't just modifying the name coming from the database so that it doesn't match a file on disk?
0
 

Author Comment

by:JT_SIRO
ID: 34149206
I think you're right.  I actually did an insert statement before the File.Move, so I needed to replace the single quote for the SQL statement.  Then it was messed up for when I did the File.Move.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34156589
You should use SQL parameters for you insert, then you don't need to worry about trying to escape invalid SQL characters (I assume you're using a SqlConnection/SqlCommand).  Reading back the data doesn't require any escaping/conversion.

using (SqlConnection cnx = new SqlConnection("Your Connection String"))
{
    using (SqlCommand cmd = cnx.CreateCommad())
    {
        cmd.CommandText = "INSERT INTO SomeTable (FileNameCol) VALUES(@FileName)";
        cmd.Parameters.AddWithValue("@FileName", "C:\blah\blah\file.txt").DbType = DbType.String;
        cmd.ExecuteNonQuery();
    }
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question