0

I'm trying to "move" a file and rename it using File.Move. It worked before I needed to add store files in a specific folder. Since adding the full path, I can create files, I can write to them but as soon as I try to use File.Move it gives me "NotSupportedException" an example of some of the code that I have is:

private static void myMethod(String file)
    {
        File.Delete(file);
        using (sout = new StreamWriter(pathStart + "temp.txt"))
        {
            foreach(Deck deck in deckList)
            {
                if (deck != null)
                {
                    sout.WriteLine(myString);
                    sout.WriteLine(otherString);
                    sout.Flush();
                }
            }
        }
        File.Move(pathStart + "temp.txt", pathStart + file);
        File.Delete(pathStart + "temp.txt");
    }

Any help would be greatly appreciated.

  • What is the value of `pathStart`? You should use `Path.Combine(pathStart, "temp.txt"). For reference, the exception will be thrown when `sourceFileName or destFileName is in an invalid format.` (see [documentation](https://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.110).aspx)) – RB. Nov 29 '16 at 19:55
  • pathStart is a copy and paste path to my file system. – comphunter159 Nov 29 '16 at 19:57
  • if you put breakpoints in your code for example on this line `File.Move(pathStart + "temp.txt", pathStart + file);` please update your code and show us what the this is when you highlight it using QuickWatch `pathStart + "temp.txt", pathStart + file` – MethodMan Nov 29 '16 at 20:00

1 Answers1

0

My guess is that you enter the method with a complete filepath since you delete the file first. That only makes sense if it contains the complete filepath. (your 'file' parameter)

In the Move call you try to add this file to the filepath, and there you are: an error.

I tried it, and it works as expected when entering only a filename. When I enter a complete filepath, I indeed get your error.

Frank
  • 431
  • 1
  • 5
  • 25