简体   繁体   English

C# 关闭应用程序后如何重命名当前文件名

[英]C# How to rename current file name after closing the application

im coding on c#, im trying to make when the application closing the name of the file will be changed.我在 c# 上进行编码,我试图在应用程序关闭文件名时进行更改。

my base name in the application changing every 10ticks in timer random strings and i want it to take the last string and change the main name to that string.我在应用程序中的基本名称每 10 次在计时器随机字符串中更改一次,我希望它采用最后一个字符串并将主名称更改为该字符串。

for example: the application closed and the last string in the application is xxx2345 then changing the filename to xxx2345.exe例如:应用程序关闭并且应用程序中的最后一个字符串是 xxx2345 然后将文件名更改为 xxx2345.exe

so im looking for rename filename after closing.所以我在关闭后寻找重命名文件名。

There is a difference between after closing and *when (while) * closing the application. after close 和*when (while) *关闭应用程序是有区别的。 I am assuming you want to do the second.我假设你想做第二个。

There are few options.选项很少。 One way will be to copy the exe file with a new name.Then 1. Delete the old file via shell command / windows scheduler (say 1 min in future) or 2. when the new file is opened, it deletes the older version (you will need a set formula for file names between version, or a registry / local storage for previous name)一种方法是使用新名称复制 exe 文件。然后 1. 通过 shell 命令 / windows 调度程序(例如 1 分钟后)删除旧文件或 2. 当新文件打开时,它会删除旧版本(您将需要为版本之间的文件名设置一个公式,或者为以前的名称使用注册表/本地存储)

For your question, you want to rename current file name after closing the application.对于您的问题,您想在关闭应用程序后重命名当前文件名。

You could try the following code to get it.您可以尝试以下代码来获取它。

        string str = r.Next(1000).ToString();
        string str1 = Process.GetCurrentProcess().MainModule.ModuleName;     
        Process[] ps = Process.GetProcessesByName(str1);
        if (ps.Length <= 0)
        {
            string filePath = System.Environment.CurrentDirectory; ;            
            string newFileName = filePath + "\\" + str + ".exe";                 
            File.Move(str1, newFileName);
        }

This is my tested result:这是我的测试结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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