Solved

Detect file exist or not

Posted on 2016-10-06
3
176 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
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 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 10

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 50

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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