JT_SIRO
asked on
Moving files in .NET C# invalid characters
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);
Do I need to check for backslashed too? Or does the @ take care of that?
strSourceFile = row["Filename"].ToString()
File.Move(@"C:\SHAT_TRACKS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
}
ASKER