Solved

Detect file exist or not

Posted on 2016-10-06
3
127 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 49

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now