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
Solved

Moving files in .NET C# invalid characters

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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