【问题标题】:How to retrieve Excel file inside project folder?如何在项目文件夹中检索 Excel 文件?
【发布时间】:2020-03-18 17:36:09
【问题描述】:

使用以下代码,我可以将 Book1.xlsx 文件从 Application 文件夹 复制到 Desktop

vb.net 版本

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    IO.File.Copy(sourceFileName:=IO.Path.Combine(New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName).DirectoryName, "Book1.xlsx"),
                 destFileName:=My.Computer.FileSystem.SpecialDirectories.Desktop & "\Book1.xlsx")
End sub

C#版本

private void Button1_Click(object sender, RoutedEventArgs e)
{
    System.IO.File.Copy(sourceFileName: System.IO.Path.Combine(new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().FullName).DirectoryName, "Book1.xlsx"), 
                          destFileName: My.Computer.FileSystem.SpecialDirectories.Desktop + @"\Book1.xlsx");
}

请按照以下方式将Book2.xlsx文件放入项目文件夹Add > Existing Item

如何从项目文件夹复制Book2.xlsx文件并粘贴到桌面?

我的问题总结在这里:

我可以将 Book1.xlsx 文件从 Application 文件夹 复制到 Desktop

如何将 Book2.xlsx 文件从 Project 文件夹 复制到 Desktop

【问题讨论】:

  • 这不是winforms代码。

标签: c# wpf vb.net visual-studio


【解决方案1】:

请按照以下步骤将 Book2.xlsx 文件放入项目文件夹 方式:添加>现有项目

如何从项目文件夹中复制 Book2.xlsx 文件并粘贴到 桌面?

我的问题总结在这里:

我可以将 Book1.xlsx 文件从应用程序文件夹复制到桌面。

如何将 Book2.xlsx 文件从 Project 文件夹复制到桌面。

在 WPF 中,您需要使用 the Pack URIs 访问您所做的文件 ' 将 Book2.xlsx 文件按以下方式放入项目文件夹:添加 > 现有项目'

并将 Book2.xlsx Build Action 设置为“Resource”。然后,试试下面的代码。

      private void Button_Click(object sender, RoutedEventArgs e)
    {
        string excelPath = "pack://application:,,,/Xbook2.xlsx";
        StreamResourceInfo excelInfo = System.Windows.Application.GetResourceStream(new Uri(excelPath));

        using (Stream file = File.Create(@"D:\Xbook2.xlsx"))
        {
            CopyStream(excelInfo.Stream, file);
        }

        //System.IO.File.Copy(excelInfo.Stream., @"D\Xbook2.xlsx");
    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[8 * 1024];
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, len);
        }
    }

【讨论】:

  • 嗨,Yohann。感谢您的回答。解决了。你认为我应该把 Book2.xlsx 文件放到 Resource 文件夹 还是 Project 文件夹?我的意思是哪个更好?
  • 请看这张图以了解我的问题prnt.sc/q1oxck
  • 一般来说,资源文件主要是程序代码文件以外的东西,比如图片等等……
【解决方案2】:

根据目录树,我假设您的项目路径以这样的方式结束

 SolutionFolder\ProjectFolder\bin\Debug

在这种情况下,您将需要倒退 2 步,这将需要这样的东西

 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            desktopPath += "\\" + @"\Book1.xlsx";


            string fileSource = Path.GetFullPath(Path.Combine(System.IO.Path.Combine(new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().FullName).DirectoryName, ""), @"..\..\Book1.xlsx"));


            System.IO.File.Copy(fileSource, desktopPath);

因此,如果您想再次返回第三步,则可以更改

@"..\..\Book1.xlsx"

@"..\..\..\Book1.xlsx"

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2010-10-10
    • 2016-05-20
    • 2023-03-29
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多