在执行file.copy异常时无法访问正在使用的文件,在编译时未调试时代码有效

米洛斯·格鲁吉克(Milos Grujic)

当然,有许多问题与我的相似,但是没有一个问题能真正回答我的问题,也没有提出为什么它能在调试中而不是在运行时运行的问题。

我做了一个FileSystemWatcher等待在我的服务器上的目录中创建一个文件。引发一个Event,然后选择引起它的文件名,将其复制为另一个目录.csv当一步一步调试时,当我编译时一切正常,尝试中断File.copy()

class Program
{
    public static string AdresaEXD { get; set; }
    public static string AdresaCSV { get; set; }
    public static string IzvorniFajl { get; set; } //source file
    public static string KreiraniFajl { get; set; }// renamed file

    static void Main(string[] args)
    {
        GledanjeFoldera();/ watcher
    }

    static void GledanjeFoldera()
    {
        AdresaEXD = @"H:\CRT_FOR_FIS\EXD";
        AdresaCSV = @"H:\CRT_FOR_FIS\CSV";

        FileSystemWatcher CRT_gledaj = new FileSystemWatcher(AdresaEXD,"*.exd");

        // Making filters 

        CRT_gledaj.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;

        // create event handler

        CRT_gledaj.Created += new FileSystemEventHandler(ObradaITransformacijaEXD);

        // starting to watch

        CRT_gledaj.EnableRaisingEvents = true;

        // loop

        Console.WriteLine("Ako zelite da prekinete program pritisnite \'q\'.");

        while (Console.ReadLine() != "q");


    }

    private static void ObradaITransformacijaEXD(object source, FileSystemEventArgs e) // 
    {

        // 
        string NoviFajl = e.Name.Remove(e.Name.Length-4,4)+".csv";// new file name

        // create paths
         IzvorniFajl = System.IO.Path.Combine(AdresaEXD, e.Name);
        KreiraniFajl = System.IO.Path.Combine(AdresaCSV, e.Name);


       // copy file to other destination and delete after 
        System.IO.File.Copy(IzvorniFajl, KreiraniFajl, true);
        System.IO.File.Delete(IzvorniFajl);


    }
}   
塔基翁

问题在于,开始创建文件时会触发FileSystemWatcher事件。请尝试以下方法:

private static bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }

如果正在使用文件,则此函数返回true。所以你可以做一个这样的循环

while (IsFileLocked(new FileInfo(eventArgs.FullPath))) { }

然后等待退出循环以复制文件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Junit执行代码时无法访问Xml文件

执行终端命令时无法访问文件

。file执行bash中的文件,然后执行> file以清空文件内容

JSF El在编译时执行

在编译时执行C宏功能

为什么在执行此功能后,file_put_contents()行的代码未执行?

为什么当我用constexpr执行代码时,有时在编译时求值,有时在运行时求值?

我的部分代码无法访问或执行

对灯泡执行操作时,Google Home无法获得有效的助手响应

Visual Studio 在编辑器中显示源代码时检测到错误,并且在生成在调试模式下执行的代码时没有发现错误

带有VirtualProtect的PAGE_GUARD在执行访问时未引发异常

导航时未执行脚本,仅当我刷新页面时才有效

file_get_contents和readfile是否执行PHP代码?

无法在Acrobat PDF中执行File.Exit操作

ng-file-upload无法执行setRequestHeader

屏幕:无法执行“ / path / to / file”:权限被拒绝

perl <file.pl> 执行时根本没有输出

使用Linq和ODataQueryOptions时,如何使EntityFramework有效执行OrderBy?

如何解决无法访问的代码日食...如果获取异常,则“ hai”语句将不执行

将数组传递给使用-File参数执行的PowerShell脚本

如何使用Django在HTML按钮上执行file.py?

使用元帅类时代码未执行的问题

仅在使用Visual Studio调试时执行代码

从CMD Out-File运行PS1时无法访问文件

调试时 Javascript 代码正确执行,但在浏览器开发工具未运行的情况下无法运行

在Node JS中执行代码时无法通过命令访问文件

Guard let 正在执行,但是它之后的代码仍然有效?

尝试预览R Notebook时出错。代码执行有效,但转换为笔记本失败

在Hostinger主机上执行file_get_contents时发生PHP错误:无法打开流