小编ani*_*ani的帖子

如何将com或excel对象序列化为二进制格式,以便我可以将它存储在asp.net的内存流中

这是示例代码...但我得到了serializationException.

xlbook是对象,我想将此对象保存到memorystream.

unsafe public void Save(IStream stream, bool clearDirty, Excel.Workbook xlbook)
{
    try
    {
        //if (stream == null)
        //{
        //    return;
        //}
        //object data = xlbook;
        if (xlbook == null)
        {
            return;
        }
        // convert data to byteArray   


        MemoryStream memoryStream = new MemoryStream();
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        //AppDomain currentDomain = AppDomain.CurrentDomain;                        
        //currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); 


  //here im getting exception.                        
        binaryFormatter.Serialize(memoryStream, xlbook);            
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        //get memory pointer
        int cb;
        int* pcb = &cb;
        //save data
        byte[] arrayLen …
Run Code Online (Sandbox Code Playgroud)

c# asp.net assemblies memorystream object-serialization

1
推荐指数
1
解决办法
3205
查看次数

如何使用c#中的线程创建临时文件

我需要在我的Handler.ashx文件中创建一个临时文件(new.txt),但问题是我将创建的excel文件保存在new.txt中作为临时文件.问题在这里我将临时文件硬编码为" new.txt".如果不止一个用户访问同一个应用程序会发生什么.如何克服这个问题.我们可以使用线程.

示例代码..

 if (File.Exists(context.Server.MapPath("new.txt")))
            {
                File.Delete(context.Server.MapPath("new.txt"));
                xlWorkBook.SaveAs(context.Server.MapPath("new.txt"), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            }
            if (!File.Exists(context.Server.MapPath("new.txt")))
            {

                xlWorkBook.SaveAs(context.Server.MapPath("new.txt"), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            }

   string file = context.Server.MapPath("new.txt");
            byte[] myByte = File.ReadAllBytes(file);

            File.Delete(context.Server.MapPath("new.txt"));

            context.Response.Clear();
            context.Response.BinaryWrite(myByte);
            context.Response.Flush();
            context.Response.Close();  
Run Code Online (Sandbox Code Playgroud)

c# multithreading httphandler

0
推荐指数
1
解决办法
319
查看次数