Click here to Skip to main content
15,663,341 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Colleagues,

I'm just new on this programing, doing just for fun and self learning.
I'm creating a a folder and a excel file every time i run my routine and its has been working just fine.
But i cannot find a way to save this excel file inside my folder.

I would like to create my folder and save my excel file inside it, of course my folder name will be different every time.

any ideas? Thank you very much.

Thanks a lot in advance.

What I have tried:

Directory.CreateDirectory("c:\\temp\\" + foldername.Text);

sheet.SaveAs("c:\\Temp\\" + excelname.Text + DateTime.Now.ToString("_yyyyMMdd"));
Updated 20-Jan-23 7:37am
Richard Deeming 17-Aug-20 11:24am    
You have already posted this:
How do I create a folder to save my excel file inside this folder using C#[^]

If you want to update your question to add missing information, click the green "Improve question" link and update your question. Do not post your update as a new question.

It can be as simple as this:
using System.IO;

namespace WFTemplate_Aug2020
    public static class public_static_class_FileExtensions
        public static FileStream NewFile(this string folderpath, string filename, string extension)
           return File.Create($"{folderpath}{filename}{extension}");
Sample call:
FileStream fs = @"C:\Users\test_user\Desktop\New Stuff\".NewFile("xray", ".txt");

0 this code will write over an existing file with the same name and path

1 this code will not intercept possible errors in file access like 'permission denied, or the file existing and being read-only.

2 there's no checking here for the easy to make mistakes in using // file path separators, and no checking for existing valid directory structure names.

See the other possible exceptions here: [^]

"Real world code" requires you address these concerns !

The other answers here show you how to check for a Directory existing.
Share this answer
[no name] 15-Aug-20 13:32pm    
Something overkill for a c# beginner and also otherwise. On the other hand a nice/interesting example to explain extentions. So have a 5.
Sandeep Mewara 15-Aug-20 14:40pm    
Take my 5! :thumbsup:
Of course you can do it using Directory.CreateDirectory Method (System.IO) | Microsoft Docs[^]

public class CreateFileOrFolder
    static void Main()
        // Specify a name for your top-level folder.
        string folderName = @"c:\Top-Level Folder";

        // To create a string that specifies the path to a subfolder under your
        // top-level folder, add a name for the subfolder to folderName.
        string pathString = System.IO.Path.Combine(folderName, "SubFolder");

        // You can write out the path name directly instead of using the Combine
        // method. Combine just makes the process easier.
        string pathString2 = @"c:\Top-Level Folder\SubFolder2";

        // You can extend the depth of your path if you want to.
        //pathString = System.IO.Path.Combine(pathString, "SubSubFolder");

        // Create the subfolder. You can verify in File Explorer that you have this
        // structure in the C: drive.
        //    Local Disk (C:)
        //        Top-Level Folder
        //            SubFolder

        // Create a file name for the file you want to create.
        string fileName = System.IO.Path.GetRandomFileName();

        // This example uses a random string for the name, but you also can specify a particular name.
        //string fileName = "MyNewFile.xls";

        // Use Combine again to add the file name to the path.
        pathString = System.IO.Path.Combine(pathString, fileName);

        // Verify the path that you have constructed.
        Console.WriteLine("Path to my file: {0}\n", pathString);

        // Check that the file doesn't already exist. If it doesn't exist, create
        // the file and write integers 0 - 99 to it.
        // DANGER: System.IO.File.Create will overwrite the file if it already exists.
        // This could happen even with random file names, although it is unlikely.
        if (!System.IO.File.Exists(pathString))
            using (System.IO.FileStream fs = System.IO.File.Create(pathString))
                for (byte i = 0; i < 100; i++)
    // Sample output:
    // Path to my file: c:\Top-Level Folder\SubFolder\ttxvauxe.vv0

Reference: How to create a file or folder - C# Programming Guide | Microsoft Docs[^]

Please tryout the example. Almost all of it is there what you shared.
Share this answer
BillWoodruff 15-Aug-20 8:31am    
Sandeep Mewara 15-Aug-20 14:40pm    

Another colleague suggested a solution that worked well for my application, please see the code below.

string foldername = foldername.Text;
string myDir = @"c:\temp";
string dirPath = Path.Combine(myDir, foldername);

//Specific here where the reporter should be saved

sheet.SaveAs(Path.Combine(dirPath, excelname.Text + DateTime.Now.ToString("_yyyyMMdd")));
Share this answer
Richard Deeming 17-Aug-20 11:24am    
You forgot to post any code.
ncostasilva 17-Aug-20 22:31pm    
Sorry, now the code is posted. sorry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900