Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detect file exist or not

Posted on 2016-10-06
3
Medium Priority
?
233 Views
Last Modified: 2016-10-06
Hi,
I want to check if relevant SpreadsheetDocument is existing or not, per given filepath (having both full path and document name). Only if the file does not exist, then I create it. Otherwise, I just open it. What to adjust below?
        public static void CreateSpreadsheetWorkbook(string filepath,string SheetName0)
        {
                SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                    Create(filepath, SpreadsheetDocumentType.Workbook);

Open in new window

0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 1500 total points
ID: 41831303
try use System.IO.File.Exists to check the existence of the file, like:

public static void CreateSpreadsheetWorkbook(string filepath,string SheetName0)
        {
            if (System.IO.File.Exists(filepath) == false) { // file does not exist
                SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                    Create(filepath, SpreadsheetDocumentType.Workbook);

more info:
File.Exists
https://www.dotnetperls.com/file-exists
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 41831319
Thanks.
How to resolve

Error	1	Embedded statement cannot be a declaration or labeled statement	C:\App\WindowsFormsApplication9_4_0\WindowsFormsApplication2\Form1.cs	389	21	WindowsFormsApplication2

Open in new window

due to last line below?
        public static void CreateSpreadsheetWorkbook(string filepath,string SheetName0)
        {
                SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                    Create(filepath, SpreadsheetDocumentType.Workbook);

                if (System.IO.File.Exists(filepath) == false)
                {
                    // Create a spreadsheet document by supplying the filepath.
                    // By default, AutoSave = true, Editable = true, and Type = xlsx.
                    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                        Create(filepath, SpreadsheetDocumentType.Workbook);
                }
                else
                    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filepath, false);

Open in new window

0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41831323
try this instead (quick guess):
        public static void CreateSpreadsheetWorkbook(string filepath,string SheetName0)
        {
                if (System.IO.File.Exists(filepath) == false)
                {
                    // Create a spreadsheet document by supplying the filepath.
                    // By default, AutoSave = true, Editable = true, and Type = xlsx.
                    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                        Create(filepath, SpreadsheetDocumentType.Workbook);
                }
                else 
              {
                    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filepath, false);
            }
}

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question