简体   繁体   中英

How to find a file automatically without actually knowing the full path file in directory in C# windows application Form

I need help with my c# application, my application is basicly done just need a minor tweak and should be done.

My program does many functionalities. but the one I need help is the following:

My program will let the user first create a excel file, there I have three buttons first button is open that will open the created file created by the user and the user will save the file in any location, but when the button open needs to find that file to where the user saved the file. right now button will work if I manually put the location of specific location of the file. but my application will be use it many users. and each user will save their own file in any directory location. I also have a button that will save data to the excel file and also to close it but those work fine as long the open file works.

this part of the code will create and save to a user location of desired. then the open button function need to automatically find the just recent file created from the computer directory. as I said before if I put the specific location the open button works but i don't want to put a specific location of the file because the user will choose that location of the excel file.

    private void tlpMenuItem_SaveAs_Click(object sender, EventArgs e)
    {


        string sd;
        svFileDialog_SaveButton.ShowDialog();
        //saveFileDialog1.InitialDirectory = "c:";
        svFileDialog_SaveButton.Filter = "Excel File|*.xlsx|All Files|*.*";
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        ExcelApp.Columns.ColumnWidth = 20;
        sd = svFileDialog_SaveButton.FileName;
        ExcelApp.ActiveWorkbook.SaveCopyAs(sd + ".xlsx");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
        MessageBox.Show("Excel file created");

    }
    private void OpenFile()
    {

        string findFile = "";
        xlexcel = new Excel.Application();

        xlexcel.Visible = true;

        // Open a File
        xlWorkBook = xlexcel.Workbooks.Open("C:\MyFile.xlsx", 0, true, 5, "", "", true,
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlWorkSheet.Cells[1, 1] = "Username";
        xlWorkSheet.Cells[1, 2] = "Password";
        xlWorkSheet.Cells[1, 3] = "Warehouse Location";
        xlWorkSheet.Cells[1, 4] = "Date";
    }

Here is my full code of the created file, open, save data and close excel file

    private void tlpMenuItem_SaveAs_Click(object sender, EventArgs e)
    {


        string sd;
        svFileDialog_SaveButton.ShowDialog();
        //saveFileDialog1.InitialDirectory = "c:";
        svFileDialog_SaveButton.Filter = "Excel File|*.xlsx|All Files|*.*";
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        ExcelApp.Columns.ColumnWidth = 20;
        sd = svFileDialog_SaveButton.FileName;
        ExcelApp.ActiveWorkbook.SaveCopyAs(sd + ".xlsx");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
        MessageBox.Show("Excel file created");

    }
    private void OpenFile()
    {

        string findFile = "";
        xlexcel = new Excel.Application();

        xlexcel.Visible = true;

        // Open a File
        xlWorkBook = xlexcel.Workbooks.Open("C:\MyFile.xlsx", 0, true, 5, "", "", true,
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlWorkSheet.Cells[1, 1] = "Username";
        xlWorkSheet.Cells[1, 2] = "Password";
        xlWorkSheet.Cells[1, 3] = "Warehouse Location";
        xlWorkSheet.Cells[1, 4] = "Date";
    }
    private void SaveDataToAFile()
    {
        int _lastRow = xlWorkSheet.Range["A" + xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;

        xlWorkSheet.Cells[_lastRow, 1] = txt_Username.Text;
        xlWorkSheet.Cells[_lastRow, 2] = txt_Password.Text;
        xlWorkSheet.Cells[_lastRow, 3] = cmb_DatabaseSelection.SelectedIndex.ToString();
        xlWorkSheet.Cells[_lastRow, 4] = DateTime.Now;
    }

    private void CloseFile()
    {
        xlWorkBook.Close(true, misValue, misValue);
        xlexcel.Quit();

        ReleaseObject(xlWorkSheet);
        ReleaseObject(xlWorkBook);
        ReleaseObject(xlexcel);
    }

    private void ReleaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

My suggestion is to create a variable to save the file path when the user saved. You can do so by using:

    String filepath; //global variable

    Stream stream;
    SaveFileDialog sf = new SaveFileDialog();
    if(sf.ShowDialog() == true)
    {
            if((stream = sf.OpenFile()) != null)
            {
                    filepath = sf.FileName; 
                    //Do save work
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM