简体   繁体   中英

Moving files into folder (C#)

I want to move a file if it exists into a folder with the date appended to the file when moved. I'm getting the error "The given path's format is not supported" though I have converted the date to a string, this is inside an event which triggers this code.

XAML:

<add key ="Directory" value ="C:\FILE WATCHER TEST FOLDER\"/>
<add key ="File name" value ="update1"/>
<add key="extension" value =".txt"/>
<add key ="Folder name" value ="archive updates\"/>

Code behind:

        DateTime date = DateTime.Parse("17/08/2012",ukCulture.DateTimeFormat);
        string theDate = Convert.ToString(date);

        string directory = ConfigurationManager.AppSettings["Directory"];
        string file = ConfigurationManager.AppSettings["File name"];
        string folder = ConfigurationManager.AppSettings["Folder name"];
        string extension = ConfigurationManager.AppSettings["extension"];

        string file_exe = file + extension;  

        string file_theDate = file + "-" + theDate;
        string file_theDate_exe = file_theDate + extension;

        string dir_fol = System.IO.Path.Combine(directory, folder);
        string dir_file_exe = System.IO.Path.Combine(directory, file_exe);
        string dir_file_theDate_exe = System.IO.Path.Combine(dir_fol, file_theDate_exe);

        if (File.Exists(dir_file_exe))
        {
            update.readNewFile();
            update.updatePaf();



            if (!Directory.Exists(dir_fol))
            {
                //create it move it
                System.IO.Directory.CreateDirectory(dir_fol);
                File.Move(dir_file_exe,dir_file_theDate_exe);


            }
            else
            {
                // move it
                if (File.Exists(dir_file_exe))
                {
                    File.Move(dir_file_exe, dir_file_theDate_exe);
                }
DateTime date = DateTime.Parse("17/08/2012",ukCulture.DateTimeFormat);

You can't have forward slashes in your path.

You could remove them (along with other illegal characters) before using the path, check out this ie: How to remove illegal characters from path and filenames?

Please use theDate.ToString({format as you need}) . May be format of date in your locale contains "bad" symbols(slashes).

Formats can be found at http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM