【问题标题】:Check if file with name exists and than save with other name检查具有名称的文件是否存在,而不是使用其他名称保存
【发布时间】:2013-04-09 12:16:41
【问题描述】:

我目前正在开发一个 Outlook 插件,它将电子邮件保存到在线共享点, 但在保存它们之前,我需要检查是否已经存在同名文件,因此它不会覆盖任何内容,这是保存文件的方法:

            {
            currExplorer = OutlookApp.ActiveExplorer();
            selection = currExplorer.Selection;
            if (selection != null)
            {
                SharePointHelper spHelper = new SharePointHelper("LoginName", "Password", "Url/FolderDirectory");
                if (selection.Count > 0)
                {
                    for (int i = 1; i <= selection.Count; i++)
                    {
                        var item = selection[i] as Outlook.MailItem;
                        if (item == null) 
                            continue;

                        // Check for attachments and save
                        currMail = item;

                        string fileName = String.Format("{0} - {1}.msg", SafeFileName(currMail.SenderName), SafeFileName(currMail.Subject));
                        string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
                        currMail.SaveAs(filePath, Outlook.OlSaveAsType.olMSG);

                        System.Net.HttpStatusCode status = spHelper.UploadFile(filePath, fileName); 
                        if (status != System.Net.HttpStatusCode.Created)
                            MessageBox.Show(fileName + " failed to upload.");
                    }
                }
            }
        }

由于我是初学者,我缺乏如何去做的经验, 衷心感谢您的帮助,谢谢大家!

【问题讨论】:

  • if (File.Exists(filename))...
  • 嗨,马修,这是否也适用于 SharePoint,因为我遇到了一些问题

标签: c# sharepoint outlook save filenames


【解决方案1】:

您可以使用 System.IO.File.Exists(string path) 来确定它是否存在。 如果是,您可以更改其名称并测试新名称是否已存在,直到您找到尚未使用的名称。

您可以在 msdn 上查看文档:http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

【讨论】:

  • 这是否也适用于 SharePoint,因为我遇到了一些问题
  • 如果您有文件的物理路径,它将起作用。即:c:\temp\x.txt
【解决方案2】:

如果您想检查 SharePoint 文件,可以使用 Microsoft.SharePoint.Client.Web 和方法 GetFileByServerRelativeUrl(string serverRelativeUrl) 查看 GetFileByServerRelativeUrl

Getting sharepoint excel file from sharepoit libraries using Client object model in asp.net

将文件从库加载到对象后,您可以进行!= null 检查,如果文件不为空,则可以更改文件名。

【讨论】:

    【解决方案3】:

    使用System.IO.File.Exists(string path)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 2014-05-23
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多