Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Winforms BackgroundWorker复制和更改文件系统.IO.u Error.WinIOError_C#_Winforms_File Io_Backgroundworker - Fatal编程技术网

C#Winforms BackgroundWorker复制和更改文件系统.IO.u Error.WinIOError

C#Winforms BackgroundWorker复制和更改文件系统.IO.u Error.WinIOError,c#,winforms,file-io,backgroundworker,C#,Winforms,File Io,Backgroundworker,我有一个带有backgroundworker(bgw)的windows窗体应用程序。 此bgw执行一些任务,其中包括: 步骤2-使用以下代码将文件从一个文件夹复制到另一个文件夹: public static void CopiarArquivos() { string fileName; string sourcePath = System.Windows.Forms.Application.StartupPath; const s

我有一个带有backgroundworker(bgw)的windows窗体应用程序。 此bgw执行一些任务,其中包括: 步骤2-使用以下代码将文件从一个文件夹复制到另一个文件夹:

    public static void CopiarArquivos()
    {
        string fileName;
        string sourcePath = System.Windows.Forms.Application.StartupPath;
        const string targetPath = @"C:\CWEB\CwebIntegracaoMovel";

        if (sourcePath == targetPath) return;

        string destFile;

        // To copy a folder's contents to a new location: 
        // Create a new target folder, if necessary. 
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        if (Directory.Exists(sourcePath))
        {
            string[] files = Directory.GetFiles(sourcePath);
            //Invoke((MethodInvoker)(() => {

            //});
            // Copy the files and overwrite destination files if they already exist. 
            foreach (string s in files)
            {
                FileInfo fi = new FileInfo(s);

                // Use static Path methods to extract only the file name from the path.
                fileName = Path.GetFileName(s);
                destFile = Path.Combine(targetPath, fileName);
                if (!ArquivosIguais(s, destFile) &&
                    (!fileName.Contains("WizardIntegracao.exe") ||
                    (fileName.Contains("WizardIntegracao.exe") && !File.Exists(destFile))))
                {
                    fi.CopyTo(destFile, true);
                    //File.Copy(s, destFile, true);
                    //new System.Security.Permissions.FileIOPermission(
                    //    System.Security.Permissions.FileIOPermissionAccess.Read, new string[] { destFile }).Demand();
                }
            }
        }
    }
public static void AlteraBaseAddress(string porta)
    {
        string path = "C:\\CWEB\\CwebIntegracaoMovel\\ServicoIntegracao.exe.config";
        string conteudo = File.ReadAllText(path);
        int inicio = conteudo.IndexOf("http://");
        int fim = conteudo.IndexOf("/ServicoIntegracao/Servico");
        conteudo = conteudo.Remove(inicio, fim - inicio);
        conteudo = conteudo.Insert(inicio, String.Format("http://localhost:{0}", porta));

        File.WriteAllText(path, conteudo);
    }
副本工作正常(有一些行被注释掉了,因为我已经测试了其他一些东西) 在bgw的步骤4中,我更改了在步骤2中复制的.config文件的一些设置,代码如下:

    public static void CopiarArquivos()
    {
        string fileName;
        string sourcePath = System.Windows.Forms.Application.StartupPath;
        const string targetPath = @"C:\CWEB\CwebIntegracaoMovel";

        if (sourcePath == targetPath) return;

        string destFile;

        // To copy a folder's contents to a new location: 
        // Create a new target folder, if necessary. 
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        if (Directory.Exists(sourcePath))
        {
            string[] files = Directory.GetFiles(sourcePath);
            //Invoke((MethodInvoker)(() => {

            //});
            // Copy the files and overwrite destination files if they already exist. 
            foreach (string s in files)
            {
                FileInfo fi = new FileInfo(s);

                // Use static Path methods to extract only the file name from the path.
                fileName = Path.GetFileName(s);
                destFile = Path.Combine(targetPath, fileName);
                if (!ArquivosIguais(s, destFile) &&
                    (!fileName.Contains("WizardIntegracao.exe") ||
                    (fileName.Contains("WizardIntegracao.exe") && !File.Exists(destFile))))
                {
                    fi.CopyTo(destFile, true);
                    //File.Copy(s, destFile, true);
                    //new System.Security.Permissions.FileIOPermission(
                    //    System.Security.Permissions.FileIOPermissionAccess.Read, new string[] { destFile }).Demand();
                }
            }
        }
    }
public static void AlteraBaseAddress(string porta)
    {
        string path = "C:\\CWEB\\CwebIntegracaoMovel\\ServicoIntegracao.exe.config";
        string conteudo = File.ReadAllText(path);
        int inicio = conteudo.IndexOf("http://");
        int fim = conteudo.IndexOf("/ServicoIntegracao/Servico");
        conteudo = conteudo.Remove(inicio, fim - inicio);
        conteudo = conteudo.Insert(inicio, String.Format("http://localhost:{0}", porta));

        File.WriteAllText(path, conteudo);
    }
当我运行应用程序时,会出现以下异常:

Ocorreu um erro no processo de instalação do serviço:
O acesso ao caminho 'C:\CWEB\CwebIntegracaoMovel\ServicoIntegracao.exe.config' foi negado..
   em System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   em System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   em System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   em System.IO.StreamWriter.CreateFile(String path, Boolean append)
   em System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   em System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
在尝试更改.config文件内容时出错,据我所知,在复制文件时出错,它们被我自己的应用程序锁定,然后我无法覆盖其内容


有人知道如何进行此操作吗?

要更改应用程序配置,应使用System.configuration.configuration类

有一个完整的示例说明如何使用Save方法实现这一点


祝您好运。

我实际上没有尝试使用配置类,而是尝试实例化一个XmlDocument来操作属性,并用xmlDoc.save(…)保存它;我会试试你的建议。。。谢谢…更改connectionString确实有效,但由于此文件是windows服务配置文件,因此我无法找到如何更改其中的其他标记。我想更改baseAddress属性。我将对此进行一些搜索…你应该将异常翻译成英语,它说的一个字都不懂。@HansPassant此.config文件不是我正在运行的主要应用程序的文件,它是我的应用程序安装的Windows服务的文件(在做其他事情的同时)@KingKing抱歉没有将其翻译成英语,是这个“对路径“…”的访问被拒绝…”