Solved

Detect file exist or not

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

805 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