如何检查文件路径以查看以前是否已保存文档,如果已保存,则更新文档(自动保存命令)。救命!! [英] How do I check a file path to see if a document has been previously saved, and if so, update the document (automated Save command). Help!!

查看:110
本文介绍了如何检查文件路径以查看以前是否已保存文档,如果已保存,则更新文档(自动保存命令)。救命!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个保存按钮,它会在第一次点击时提示SaveFileDialogue(用户命名文件等)以及保存按钮再次单击,文件被重新保存(更新),并且不会再次提示SFD。



我创建了一个名为filepath的私有字符串并设置它为空。

  private   string  filepath =  null ; 





这是我的另存为代码:

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





其中 SaveDoc 是我的SaveFileDialogue的名字。



对于我的保存按钮代码,到目前为止,我有这个,但我输了。



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

}





我正在设计保存按钮以检查如果filepath为null,如果是,则将激活Save As代码。否则,该文件将更新。理论上很简单,但实际上并非如此......有什么建议吗?

解决方案

C#区分大小写,所以如果你想要

  private   string  filepath =  null ; 

并且

 CreateWordDocument(FilePath,SaveDoc.FileName); 

要引用相同的变量,则需要在两者上使用相同的大小写。如果你不这样做,那么拥有两个具有相似名称的变量是一个坏主意!



由于你所显示的代码都没有修改<$的内容c $ c> filepath 它总是为空。您是否要在从Save按钮处理程序返回之前将filepath设置为FilePath或SaveDoc.FileName?





[ 1]当用户第一次保存文件时,SFD将返回用户选择的文件路径(带文件名)。将此路径存储在某个变量中。[2]再次单击保存按钮时,检查该路径变量,如果有值则跳过SFD并进行更新逻辑。[3]当用户/文件会话完成时,在代码中的适当位置清除此路径变量。





从更改名称开始! :笑:

拥有保存路径的变量的想法是好的 - 它可以工作,但只有当你真正存储文件路径时才会这样。

所以,伪代码!

保存功能:

如果Filename不为null 

将文件保存到Filename
else
使用SaveFileDialog获取用户选择。
如果用户按下OK
然后
将文件名设置为用户选择
将文件保存到文件名
结束
结束



另存为函数:

使用SaveFileDialog获取用户选择。 
如果用户按下OK
然后
设置文件名到用户选择
保存文件
结束



在你的case,你不需要在任何时候清除Filename,除了把它作为null。

转换为C#应该很简单,是吗?


< blockquote>



看一下 System.IO.File.Exists方法 [ ^ ],其目的是确定path参数指定的文件是否存在。



在您的示例中,您可以通过以下方式使用此方法:

  //  将filePath变量设置为空字符串...  
private string filepath = string .Empty;





当您想要保存文件时:

  if ((filepath.length!=  string  .Empty )&& (File.Exists(filepath))){
// 在此处实现保存现有文件例程。 ..
} 其他 {
// 在这里实现保存新文件例程...
if (SaveDoc.ShowDialog()== DialogResult.OK)
{
CreateWordDocument(filepath,SaveDoc.FileName);
tEnabled( false );
return ;
}

}





...希望它有所帮助。


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.

private string filepath = null;



Here is my Save As code:

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.

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?

解决方案

C# is case sensitive, so if you wanted

private string filepath = null;

And

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?


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:

//Set your filePath variable to a null string ... 
private string filepath = string.Empty;



and when you are looking to save your file:

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.


这篇关于如何检查文件路径以查看以前是否已保存文档,如果已保存,则更新文档(自动保存命令)。救命!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆