zoukankan      html  css  js  c++  java
  • 文件操作

    1.判断文件是否处于打开状态下

    [DllImport("kernel32.dll")]
            public static extern IntPtr _lopen(string file, int readwrite);
    
            [DllImport("kernel32.dll")]
            public static extern bool CloseHandle(IntPtr hObject);
    
            public const int OF_READWRITE = 2;
            public const int OF_SHARE_DENY_NONE = 0x40;
            public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);
    
            public static bool isFileOpen(string file)
            {
                if (!File.Exists(file))
                    return false;
                IntPtr vhandle = _lopen(file, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vhandle == HFILE_ERROR)
                    return true;
                CloseHandle(vhandle);
                return false;
            }
    

     2.保存成csv文件

    private void button1_Click(object sender, EventArgs e)
            {
                
                string path = @"d:aa.csv";
                if (isFileOpen(path))
                {
                    MessageBox.Show("文件处于打开状态,无法写入.");
                    return;
                } 
                StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
                for (int i = 0; i < 100; i++)
                    sw.WriteLine($"第{i}次,aaaa,bbbb,cccc,");//中间用逗号隔开,可以分多个栏位
                sw.Flush();
                sw.Close();
    
            }
  • 相关阅读:
    学习进度笔记
    博雅数据机器学习07
    学习进度笔记
    博雅数据机器学习06
    《一级架构师》阅读笔记
    学习进度笔记
    博雅数据机器学习05
    递归的概念
    CSS学习笔记3:选择器及优先级
    CSS学习笔记2:伪类
  • 原文地址:https://www.cnblogs.com/yagzh2000/p/13600791.html
Copyright © 2011-2022 走看看