Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4242408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:26:16+00:00 2026-05-21T03:26:16+00:00

In debug mode, while running the C# WinForms App, I successfully select multiple files

  • 0

In debug mode, while running the C# WinForms App, I successfully select multiple files through the OpenFileDialog, which is then displayed in the logging window, these files are copied to a temp directory and I believe I get the error when trying to convert the excel files to csv. I get the following runtime debug error:

Error: You may not have permission to read the file or it may be corrupt. 
Reported Error: Length Can not be less than zero. 
Parameter Name: Length.

How do I fix this error?

Here’s my code on MainForm.cs

            // Consolidate Button Click Commands that executes if there are no user input errors 
    void ExecuteConsolidate()
    {
        string consolidatedFolder = targetFolderBrowserDialog.SelectedPath;
        string tempfolder = targetFolderBrowserDialog.SelectedPath + "\\tempDirectory";
        string sFile = "";

        //create a temporary directory to store selected excel and csv files
        if (!Directory.Exists(tempfolder))
        {
            Directory.CreateDirectory(tempfolder);
        }       
        try
        {
            for (int i = 0; i < listBoxSourceFiles.Items.Count; i++)
            {
                sFile = listBoxSourceFiles.Items[i].ToString();
                // Copy each selected xlsx files into the specified Temporary Folder 
                System.IO.File.Copy(textBoxSourceDir.Text + "\\" + sFile, tempfolder + @"\" + System.IO.Path.GetFileName(sFile), true);
                Log("File " + sFile + " has been copied to " + tempfolder + @"\" + System.IO.Path.GetFileName(sFile));                                                                          
            } // ends foreach    

            Process convertFilesProcess = new Process();
            // remove xlsx extension from filename so that we can add the .csv extension 
            string csvFileName = sourceFileOpenFileDialog.FileName.Substring(0, sourceFileOpenFileDialog.FileName.Length - 3);

            // command prompt execution for converting xlsx files to csv
            convertFilesProcess.StartInfo.WorkingDirectory = "I:\\CommissisionReconciliation\\App\\ConvertExcel\\";
            convertFilesProcess.StartInfo.FileName = "ConvertExcelTo.exe";
            convertFilesProcess.StartInfo.Arguments = " ^ " + targetFolderBrowserDialog.SelectedPath + "^" + csvFileName + ".csv";
            convertFilesProcess.StartInfo.UseShellExecute = true;
            convertFilesProcess.StartInfo.CreateNoWindow = true;
            convertFilesProcess.StartInfo.RedirectStandardOutput = true;
            convertFilesProcess.StartInfo.RedirectStandardError = true;
            convertFilesProcess.Start();

            //Process that creates all the xlsx files in temp folder to csv files.
            Process consolidateFilesProcess = new Process();

            // command prompt execution for CSV File Consolidation
            consolidateFilesProcess.StartInfo.WorkingDirectory = targetFolderBrowserDialog.SelectedPath;
            consolidateFilesProcess.StartInfo.Arguments = "Copy *.csv ^" + csvFileName + ".csv";
            consolidateFilesProcess.StartInfo.UseShellExecute = false;
            consolidateFilesProcess.StartInfo.CreateNoWindow = true;
            consolidateFilesProcess.StartInfo.RedirectStandardOutput = true;
            consolidateFilesProcess.StartInfo.RedirectStandardError = true;
            consolidateFilesProcess.Start();

            Log("All Files at " + tempfolder + " has been converted to a csv file");
            Thread.Sleep(2000);
            StreamReader sOut = consolidateFilesProcess.StandardOutput;
            sOut.Close();

        }
        catch (SecurityException ex)
        {
            // The user lacks appropriate permissions to read files, discover paths, etc.
            MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" +
            "Error message: " + ex.Message + "\n\n");
        }
        catch (Exception ex)
        {
            // Could not load the image - probably related to Windows file system permissions.
            MessageBox.Show("You may not have permission to read the file, or " +
             "it may be corrupt.\n\nReported error: " + ex.Message);
        }
        try
        {                
            if (Directory.Exists(tempfolder))
            {
                Directory.Delete(tempfolder, true);
            }
        }
        catch (SecurityException ex)
        {
            // The user lacks appropriate permissions to read files, discover paths, etc.
            MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" +
            "Error message: " + ex.Message + "\n\n");
        }
        catch (Exception ex)
        {
            // Could not load the image - probably related to Windows file system permissions.
            MessageBox.Show("You may not have permission to read the file, or " +
             "it may be corrupt.\n\nReported error: " + ex.Message);
        }
        finally
        {
            // reset events
            m_EventStopThread.Reset();
            m_EventThreadStopped.Reset();

            // create worker thread instance;
            m_WorkerThread = new Thread(new ThreadStart(this.WorkerThreadFunction));
            m_WorkerThread.Start();
        }
    } // ends void ExecuteConsolidate()

Thanks for looking! 🙂
All helpful answers will receive up-votes! 🙂
If you need more information like the workerThread method or the app.config code, let me know!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:26:17+00:00Added an answer on May 21, 2026 at 3:26 am

    I suppose it dies here:

    string csvFileName = sourceFileOpenFileDialog.FileName.Substring(0, 
               sourceFileOpenFileDialog.FileName.Length - 3);
    

    Write it like this and see if it helps:

    string selectedFile = sourceFileOpenFileDialog.FileName;
    string csvFileName = Path.Combine(Path.GetDirectoryName(selectedFile), 
               Path.GetFileNameWithoutExtension(selectedFile));
    

    This is the translation of your line.

    But I think, you really wanted to just have the filename without path:

    string csvFileName = 
             Path.GetFileNameWithoutExtension(sourceFileOpenFileDialog.FileName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In debug mode, while running the C# WinFOrms App, After I select the files
I have an importer process which is running as a windows service (debug mode
I'm debug-running a daemon application in foreground mode inside gdb inside a tmux session.
When in debug mode, and when I press 'n', after a while it goes
Sometimes my c++ program crashes in debug mode, and what I got is a
Do C#/.NET floating point operations differ in precision between debug mode and release mode?
When I run my Visual Studio Windows Forms application by clicking F5 (debug mode),
I want to define some member variable and some code just in Debug Mode,
In most C or C++ environments, there is a debug mode and a release
Hitting debug will deploy the application to \program files\AppNamespace\AppName.exe and attach to it. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.