0

So I'm making a desktop WPF app, whose main role is to create a .txt file with the content and name given by the user in 2 different TextBoxes("textHolder" for the content and "docName" for the name).

Basically, the user writes down a name for the document, some content and after he presses the "Save" button, the app creates a .txt document with those file infos. I managed to code the content and saving part, but I'm stuck tryning to change the name of the document.

I've tried to change the name using the .MoveTo method but I can't figure out how to include the name given by the user in the string. (the following code doesn't work properly)

private void Button_Click(object sender, RoutedEventArgs e)
{
    StreamWriter sw = new StreamWriter(
        @"C:\Users\alex_\OneDrive\All\Desktop\Texts\newTextDoc.txt");

    sw.Write(textHolder.Text);
    sw.Close();

    string nameOfTheDoc = docName.Text + ".txt";
    string filePath = "C:\\Users\\alex_\\OneDrive\\All\\Desktop\\Texts";
    string pathAndName = filePath + nameOfTheDoc;

    FileInfo fi = new FileInfo(
        @"C:\Users\alex_\OneDrive\All\Desktop\Texts\newTextDoc.txt");

    if (fi.Exists)
    {
        fi.MoveTo(pathAndName);
    }
} 
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Alex Arek
  • 27
  • 3
  • 1
    `string pathAndName = Path.Combine(filePath, nameOfTheDoc);` – Rufus L Jan 07 '21 at 00:38
  • Instead of just saying, *"the following code doesn't work properly"*, it would be helpful if you described what the code is doing improperly, or if there is an exception, include the exception message. – Rufus L Jan 07 '21 at 00:39
  • In the code above, `if (fi.Exists)` will always be `true`. If you're trying to allow the user to change the name, then you may need to keep track of which name the contents were last saved as in a class field, so you can move it. – Rufus L Jan 07 '21 at 00:40
  • When combining parts of paths, consider using `Path.Combine`. It nicely takes care of some edge cases. I don't believe that there's a *SpecialFolder* (in the `Environment.SpecialFolders` enum) for the mapped OneDrive location in the file system (the OneDrive web-ish API does have this). However, this https://stackoverflow.com/questions/26771265/get-onedrive-path-in-windows might be useful. Why are you even moving things - why not just write it once? I'm not following the purpose of your code – Flydog57 Jan 07 '21 at 00:44
  • By the "the following code doesn't work properly" I meant to say that it just doesn't change the name of the document, but at the same time it doesn't give me any errors. I already said that I've managed to code the content and creating the .text file part, so the name change was the only thing left I had to figure out. I'll try to be more explicit next time, and the problem is solved. Thank you! – Alex Arek Jan 07 '21 at 00:45

1 Answers1

1

You are already creating a pathAndName for your desired outputfile. Why not just write to that filename instead of newTextDoc.txt and rename afterwards.

And your pathAndName is missing a path-separator \ between the filePath and the fileName part. But you should probably use System.IO.Path.Combine to create your filenames anyways. This will take care of separators and insert the correct ones.

private void Button_Click(object sender, RoutedEventArgs e)
{
  string nameOfTheDoc = docName.Text + ".txt";
  string filePath = System.IO.Path.Combine("c:", "users", ... ,"Texts", nameOfTheDoc);

  StreamWriter sw = new StreamWriter(filePath);
  sw.Write(textHolder.Text);
  sw.Close();
}
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • for a hard coded directory path, you may as well just use `@"C:\Users\alex_\OneDrive\All\Desktop\Texts"` as the first argument to `Combine` and `nameOfTheDoc` as the second. – Rufus L Jan 07 '21 at 00:43
  • Or look up a lot of it somewhere under `HKEY_CURRENT_USER\Software\Microsoft\OneDrive` – Flydog57 Jan 07 '21 at 00:45