| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1581 人关注过本帖
标题:c#程式写好,exe文件挂在任务计划程序下,过段时间exe文件会变大并运行报错 ...
只看楼主 加入收藏
shl615807585
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-11-23
收藏
 问题点数:0 回复次数:1 
c#程式写好,exe文件挂在任务计划程序下,过段时间exe文件会变大并运行报错,应该是哪里资源没释放,但没找到,求高手帮忙看下
//批量处理文件,把目标文件夹srcPath下的文件更名并移动到指定文件夹destPath下
static void Main(string[] args)
        {
            string[] srcPath = new string[] { @"D:\CR80", @"D:\CR81", @"D:\CR82" };
            string destPath = @"D:\CRMAPdata";
            CRmove cr = new CRmove();
            for (int i = 0; i < 3; i++)
            {
                cr.MOVE(srcPath[i], destPath);
            }
        }
class CRmove
    {
        private string[] data = new string[10];
        private string[] time = new string[20];
        private string Newtime = "";
        private string newPath = "";
        public void MOVE(string srcPath,string destPath)
        {
            foreach (string file in Directory.GetFiles(srcPath, "*.FXY", SearchOption.AllDirectories))
            {
                try
                {
                    //读取文件第一行
                    //1.创建StreamReader对象,并指定要读取的文件,与编码格式
                    StreamReader sr = new StreamReader(file, Encoding.UTF8);
                    //2.调用StreamReader对象的readline方法,读取第1行
                    string content = sr.ReadLine();
                    //根据空格分隔第一行数据,取data[3]加上文件创建时间作为新文件名
                    data = content.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    sr.Dispose();
                        Fileinfo fi = new FileInfo(file);
                        //取文件时间
                        time = fi.LastWriteTime.ToString().Split(new char[] { ' ', '/', ':' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < time.Length; i++)
                        {
                            Newtime += time[i];
                        }
                        //新文件路径
                        newPath = destPath + @"\" + data[3] + @"-" + Newtime + @".FXY";
                        Newtime = "";
                        if (!Directory.Exists(newPath))
                        {
                            //将新命名文件移到新文件夹
                            fi.MoveTo(newPath);
                            Console.WriteLine(file + "修改完成");
                        }
                }
                catch (Exception)
                {
                    Console.WriteLine(srcPath + @"\" + file + "修改失败");
                    //Console.ReadKey();
                }
            }
        }
    }
搜索更多相关主题的帖子: 文件 string file new exe 
2021-11-23 12:04
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 
1-sr流在使用后应先close
2- if (!Directory.Exists(newPath))
                        {
                            //将新命名文件移到新文件夹
                            fi.MoveTo(newPath);
                            Console.WriteLine(file + "修改完成");
                        }
你判断了目标文件夹不存在,但应先创建目标文件夹,然后再移动文件。

2022-02-21 10:10
快速回复:c#程式写好,exe文件挂在任务计划程序下,过段时间exe文件会变大并运行 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014750 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved