Solved

Moving files in .NET C# invalid characters

Posted on 2010-11-16
3
473 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
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now