如何搜索文件扩展名(.xls)并对其进行更改';她叫什么名字?C#

如何搜索文件扩展名(.xls)并对其进行更改';她叫什么名字?C#,c#,windows,C#,Windows,我使用的产品是Beijer HMI,目前我可以生成报告并将其保存到已知位置(我的桌面-C:\Users\mrdav\desktop) 我需要能够在我的桌面上搜索文件扩展名.xls并更改其名称 当HMI生成报告时,它使用日期和时间,这意味着生成文件时,名称每次都不同 按下按钮后,我需要在桌面上搜索.xls文件并将其名称更改为变量 //这是我的程序变量 字符串NewName=Globals.Tags.Tag1.Value 生成的代码需要位于下面的示例中 public partial class Sc

我使用的产品是Beijer HMI,目前我可以生成报告并将其保存到已知位置(我的桌面-C:\Users\mrdav\desktop)

我需要能够在我的桌面上搜索文件扩展名.xls并更改其名称

当HMI生成报告时,它使用日期和时间,这意味着生成文件时,名称每次都不同

按下按钮后,我需要在桌面上搜索.xls文件并将其名称更改为变量

//这是我的程序变量

字符串NewName=Globals.Tags.Tag1.Value

生成的代码需要位于下面的示例中

public partial class Screen1
{

    void Button1_Click(System.Object sender, System.EventArgs e)
    {

        // Code to be added here... 
    }


}
希望有人能帮忙,我使用的是功能有限的windows compact framework

有任何问题请告诉我

提前感谢,


Dave

以下是一个示例,您可以这样做:

DirectoryInfo dir = new DirectoryInfo(sExportPath);
FileInfo[] Files = dir.GetFiles("*.csv"); 
foreach(FileInfo file in Files )
{
   // rename file
   System.IO.File.Move(file.FullName, GenerateNewFileName());
}
//elsewhere in the class
private string GenerateNewFileName()
{
    //here is where you implement creating or getting the filename that you want your file to be renamed to. An example might look like the below
    string serialNumber = GetSerialNumber(); //Get the serial number that you talked about in the question. I've made it a string, but it could be an int (it should be a string)
    return Path.ChangeExtension(serialNumber,".xls"); //to use path you will need a using statement at the top of your class file 'using System.IO'
}

这似乎很管用……但我知道它并没有那么整洁

有什么建议吗

多亏了大家的帮助,我们终于到达了目的地

    void Button_Click(System.Object sender, System.EventArgs e)
    {
        try
        {
        // Location for new file
            string NewFileName = @"c:\users\mrdav\desktop\testfolder\";
        // Add varibale name to new file
            NewFileName += Globals.Tags.Tag1.Value;
        // add .xls extention to new file
            NewFileName += ".xls";
        //show new file name to check all ok
            MessageBox.Show (NewFileName);
        //search for .xls in known directory
            DirectoryInfo di = new DirectoryInfo(@"c:\users\mrdav\desktop");
            FileInfo[] Files = di.GetFiles("*.xls");
        // if files exist with .xls extention
            foreach(FileInfo file in Files )
            {
            // show full file name
                MessageBox.Show (file.FullName);
            //rename old file to new file name and move to new folder
                File.Move(file.FullName, NewFileName);
            }
        }
        catch (Exception ex)

        {
            MessageBox.Show (ex.ToString());
        }
    }

请在您的问题中包含描述问题和澄清问题的最小文本量Hi Eugene。。感谢您的反馈,希望我的编辑对您更有意义。如果你需要更多信息,请告诉我。祝你一切顺利。数据此文件放入的目录,是专用于该产品,还是直接放在与桌面一样开放的地方?文件在这里被删除的频率是多少?你多久运行一次这个脚本?我编辑了我的原始帖子来回答你的一个问题。我使用的产品是Beijer HMI。该代码将在按下按钮后激活,以将文件名从10.10.10.xls更改为“tagvalue”.xls,并暂时保存在同一位置。谢谢Eugene,我是否在此处添加所需的变量(VarName)System.IO.file.Move(file.FullName,GenerateNeFileName(Var name));很抱歉,GenerateNewFileName()是一个方法(您需要创建),它将生成您要将文件重命名为的文件名。我试图实现您的代码,但它不允许使用新方法。我已经编辑了我的原始问题,所以你可以看到格式是什么。在这些代码上面,我包含了System.IO;