Solved

Moving files in .NET C# invalid characters

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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