繁体   English   中英

在C#中将数据从.DLL移到.Exe

[英]Moving Data from .DLL to an .Exe in C#

我正在重写C#DLL,以将文件直接发送到驻留在同一台计算机上的ac#可执行文件。 当前,此DLL基本将文件保存到某个位置,然后可执行应用程序使用File.ReadAllText()命令选择该文件。

实现功能的最佳方法是什么? 目前,我已经在C#中研究AnonymousPipes,并且能够轻松地通过管道将文本文件传输到可执行文件。 例如,如果要传输图像文件怎么办?

到目前为止,我已经尝试使用C#AnonymousPipes。

                using (PipeStream pipeClient =
                    new AnonymousPipeClientStream(PipeDirection.In, args[0]))
                {
                    pipeClient.ReadMode = PipeTransmissionMode.Message;
                    using (StreamReader sr = new StreamReader(pipeClient))
                    {
                        string myData = sr.ReadToEnd();
                    }
                }
            }

编辑:为进一步澄清此DLL打开可执行文件。

下面是我想出的在DLL和可执行文件之间进行通信的内容。 我对管道组件的概念还很陌生,不知道这是执行此任务的最佳方法,还是这方面的不佳实践。

Process pipeClient = new Process();

pipeClient.StartInfo.FileName = @"Executable Location";

using (AnonymousPipeServerStream pipeServer =
    new AnonymousPipeServerStream(PipeDirection.Out,
    HandleInheritability.Inheritable))
{
pipeServer.ReadMode = PipeTransmissionMode.Byte;
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
        pipeServer.GetClientHandleAsString();
    pipeClient.StartInfo.UseShellExecute = false;
    pipeClient.Start();
    pipeServer.DisposeLocalCopyOfClientHandle();
    try
    {
    Byte[] bytes = File.ReadAllBytes(@"Location of Data File");
    string file = Convert.ToBase64String(bytes);

    // Read user input and send that to the client process.
    using (StreamWriter sw = new StreamWriter(pipeServer))
    {
        sw.Flush();
        sw.Write(file);
        pipeServer.WaitForPipeDrain();
    }
}
    // Catch the IOException that is raised if the pipe is broken
    // or disconnected.
    catch (IOException a)
    {
    }
}
pipeClient.WaitForExit();
pipeClient.Close();

编辑:修正了错字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM