首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修复System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath)

如何修复System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath)
EN

Stack Overflow用户
提问于 2019-07-08 14:19:56
回答 1查看 7.4K关注 0票数 0

我正在试图复制一个文件来创建一个备份。这种情况递归地发生了几次,但是自从我进行这个递归之后,这个错误就会被抛出。

当我对一个文件执行此功能时,该函数就起作用了。

代码语言:javascript
运行
复制
public void Scan(string path)
{
    string[] libs = { "CHIP", "SOT", "SMC", "SOP", "SOJ", "QFP", "PLCC", "PLCC", "SINGLE", "SIP", "DIP", "CON", "ZIG", "BRG", "PAD", "FID" };
    try
    {
        foreach (string lib in libs) 
        {
            foreach (var filePath in Directory.EnumerateFiles(path, lib +".lib"))
            {
                //This is for debugging important in order to differ between used and unused libs
                //Here i should create a new instance of a file class that way i can find the lib's back easyly
                createBackup(filePath, txtBackup.Text);
            }
        }

        foreach (var dir in Directory.EnumerateDirectories(path)) 
        {
            Scan(dir);
        }
    }
    catch (UnauthorizedAccessException) 
    {
        Console.WriteLine("Error: " + path);
    }
}

public void createBackup(string filePath, string backUpLocation)
{
    string fileToBackUp = filePath;
    string fileName;
    int lastBackSlash = filePath.LastIndexOf("\\") + 1;
    string caption;
    string message;

    fileName = fileToBackUp.Substring(lastBackSlash, fileToBackUp.Length - lastBackSlash);

    if (System.IO.File.Exists(backUpLocation + "\\" + fileName) && chk_overWriteAllBackups.IsChecked == false)
    {
        caption = "File already present!";
        message = "Would you like to override the previous back-up that is present in this folder?";

        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;
        result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);

        if (result == System.Windows.Forms.DialogResult.Yes)
        {
            //Backup created in the selected folder and old backup is overwriten
            //Add the name of the file to the array of backed up items
            System.IO.File.Copy(filePath, backUpLocation + "\\" + fileName, true);
            readFiles();
        }
        else
        {
            caption = "Are you sure?";
            message = "No back-up will be created are you sure?";
            result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                //Continue without backups
                readFiles();
            }
            else
            {
                caption = "Select diffrent back-up location?";
                message = "Would you like to select a new back-up location?";
                result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    //Return the user so a new back-up location can be selected
                    return;
                }
                else
                {
                    //Continue without backups
                    readFiles();
                }
            }
        }
    }
    else
    {
        try
        {
  Error->          System.IO.File.Copy(filePath, backUpLocation + "\\" + fileName, true);
        }
        catch (IOException e)
        {
            caption = "Error";
            message = e.ToString();
            System.Windows.Forms.MessageBox.Show(message, caption);
        }

    }
    readFiles();
}

完全错误:

C:\Users\TomekJasinski\Documents\Hello_world\test\test\MainWindow.xaml.cs:row 99中的\r\n bij System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath)\r System.IO.File.InternalCopy(字符串sourceFileName,字符串destFileName,布尔覆盖,布尔checkHost)\\n bij System.IO.File.Copy(String sourceFileName,String destFileName,布尔覆盖)\r\n bij test.MainWindow.createBackup(String filePath,String backUpLocation)

EN

回答 1

Stack Overflow用户

发布于 2022-10-14 11:32:16

这可能与文件名长度有关--只是在visual studio中尝试打开测试日志文件时出现了类似的错误--它无法创建带有272个字符名的文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56936947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档