Click here to Skip to main content
15,885,086 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a Save button which will prompt the SaveFileDialogue upon the first click (the user names the file, etc.) and when the Save button is clicked again, the file is re-saved (updated) and the SFD isn't prompted again.

I've created a private string called filepath and set it to null.
C#
private string filepath = null;


Here is my Save As code:
C#
if (SaveDoc.ShowDialog() == DialogResult.OK)
      {
        CreateWordDocument(FilePath, SaveDoc.FileName);
        tEnabled(false);
        return;
       }


Where SaveDoc is the name of my SaveFileDialogue.

For my Save button code, I have this so far, but I'm lost.

C#
if (filepath == null)
    {
       if (SaveDoc.ShowDialog() == DialogResult.OK)
         {
           CreateWordDocument(FilePath, SaveDoc.FileName);
           tEnabled(false);
           return;
         }

     }


I'm designing the Save button to check if the filepath is null, if it is, the Save As code will be activated. Otherwise, the file will update. Simple in theory, but not in reality...yet. Any advice?
Posted
Updated 24-Sep-15 4:31am
v2

C# is case sensitive, so if you wanted
C#
private string filepath = null;
And
C#
CreateWordDocument(FilePath, SaveDoc.FileName);
To refer to the same variable, then you need to use the same case on both. If you don't, then it's a bad idea to have two variables with such similar names!

And since none of the code you show modifies the content of filepath it will alway be null. Did you mean to set filepath to FilePath or SaveDoc.FileName before you returned from your Save button handler?


"[1] When user saves file for 1st time, SFD will return filepath (with filename) that user chose. Store this path in some variable. [2] When save button is clicked again, check for this path variable, if it has value then skip SFD and do update logic. [3] Clear this path variable at appropriate location in code when user/that file session is completed."


Start off by changing the names! :laugh:
The idea of having a variable which holds the Save Path is good - and it works, but only if you do actually store the file path into it.
So, pseudo code!
Save function:
if Filename is not null
then 
   Save file to Filename
else
   Use SaveFileDialog to get user choice.
   if user pressed OK
   then
      set Filename to user choice
      Save file to Filename
   end
end

Save As function:
Use SaveFileDialog to get user choice.
if user pressed OK
then
  set Filename to user choice
  Save file
end

In your case, you don't need to clear Filename at any point, except to start it as null.
That should be pretty simple to convert to C#, yes?
 
Share this answer
 
v2
Comments
hypermellow 24-Sep-15 10:57am    
Good spot with the case differences - 5'd
Member 11909035 24-Sep-15 13:18pm    
Thanks for your reply.

Yes, I know that c# is case sensitive. Actually FilePath is used for the following code below.
string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileTemplate);

The is for the .doc file being used within the program.

Regarding your last comment about the filepath always being null, this is my first time ever trying to do this. I saw on a YouTube tutorial where somebody put a private string set to null. Watching that video and taking into consideration the advice I received in a different forum (see below), I'm trying to make sense of it all.

"[1] When user saves file for 1st time, SFD will return filepath (with filename) that user chose. Store this path in some variable. [2] When save button is clicked again, check for this path variable, if it has value then skip SFD and do update logic. [3] Clear this path variable at appropriate location in code when user/that file session is completed."
OriginalGriff 24-Sep-15 14:07pm    
Answer updated
Member 11909035 24-Sep-15 14:45pm    
Thanks again for your help. I'm not sure how to write the code for the Save function. Specifically the "Save file to Filename" line. Here's what I have so far.

if (SaveDoc.FileName != null) //I'm assuming this is the correct way to write "is not null".
{
//Save file to Filename
}
else (SaveDoc.ShowDialog() == DialogResult.OK)
{
CreateWordDocument(FilePath, SaveDoc.FileName, pathImage);
tEnabled(false);
return;
}

In your opinion, am I on the right track? Thanks!
OriginalGriff 24-Sep-15 15:08pm    
Stop guessing, and start thinking. :laugh:

myObject != null

Is correct, but why would you want to check the SaveFileDialog against null?

Think about the pseudo code I gave you, and it should be pretty obvious.
Hi,

Have a look at the System.IO.File.Exists Method[^], its purpose is to determine if a file specified by the path parameter exists.

In your example, you could make use of this method by:
C#
//Set your filePath variable to a null string ... 
private string filepath = string.Empty;


and when you are looking to save your file:
C#
if ((filepath.length != string.Empty) && (File.Exists(filepath))){
    //Implement your "save existing file" routine here ...
}else{
    //Implement your "save new file" routine here ...
       if (SaveDoc.ShowDialog() == DialogResult.OK)
         {
           CreateWordDocument(filepath, SaveDoc.FileName);
           tEnabled(false);
           return;
         }
 
     }


... hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900