Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Moving files in .NET C# invalid characters

Posted on 2010-11-16
3
Medium Priority
?
494 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 1000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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