本文介绍了将字典写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要在会话之间将数据从其Dictionary对象写入磁盘。 WriteAllBytes和ReadAllBytes将是最快的方式,但我不知道如何将字典转换为字节。



有人可以提出解决方案吗? VB.net用于我的应用程序,但也欢迎C#响应。



我尝试过:



直播,投射功能,搜索www

My app needs to write data from its Dictionary objects to disk between sessions. WriteAllBytes and ReadAllBytes would be the fastest way, but I do not know how to convert the dictionaries to bytes.

Can someone suggest a solution please? VB.net is used for my app but C# responses will be welcome too.

What I have tried:

directcast, cast functions, searching the www

推荐答案

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

class Test
{
   // A dictionary to save/load
   private Dictionary<int, string> _dictionary = new Dictionary<int, string>();

   // A method to save the dictionary to disk
   void Save(string path) {
      using (FileStream fs = new FileStream(path, FileMode.Create)) {
         BinaryFormatter formatter = new BinaryFormatter();
         formatter.Serialize(fs, _dictionary);
      }
   }

   // A method to load the dictionary from the disk
   void Load(string path) {
      using (FileStream fs = new FileStream(path, FileMode.Open)) {
         BinaryFormatter formatter = new BinaryFormatter();
         _dictionary = (Dictionary<int, string>)formatter.Deserialize(fs);
      }
   }
}



当然,您可能希望在不错的try..catch块中添加这些以便错误可以被处理。但是你有一般原则。



亲切。


Of course, you may want to add these in nice try..catch blocks so that errors can be handled. But you have the general principle.

Kindly.


Option Explicit On
Option Strict On
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Collections.Generic

Friend Class CWrapperGlobal

    Friend intlstDepToHide As New List(Of Integer)
    Friend strlstCellmodeCellAddresses As New List(Of String)
    Friend spCurrentSpecialParms As cSpecialParms
    Friend blnAbandonImportedDep As Boolean
    Friend blnBad As Boolean
    Friend blnBlankScriptOK As Boolean
    Friend blnChangingWorksheet As Boolean
    Friend blnClosingWorkbook As Boolean
    Friend blnCombinedSketchAndScript As Boolean
    Friend blnCompoundDepBeingProcessed As Boolean
    Friend blnCreateInvisible As Boolean
    Friend blnDidEditScript As Boolean
    Friend blnEstimating As Boolean
    Friend blnFirstSession As Boolean
    Friend blnMainDrawSomeSelected As Boolean
    Friend blnOkForPolyshape() As Boolean
    Friend blnPleaseRedraw As Boolean
    Friend blnValueIsOkay As Boolean
    '****************************
    Friend CellRange As myCellRange
    Friend CellRanges() As myCellRange
    Friend Stopwatch As Diagnostics.Stopwatch = New Diagnostics.Stopwatch
    Friend StopWatchAddLine As Diagnostics.Stopwatch = New Diagnostics.Stopwatch
    Friend StopWatchAddShape As Diagnostics.Stopwatch = New Diagnostics.Stopwatch
    '********************************
    'keep certain dictionaries out of pwrap to reduce its bloat
    Friend DictDragged As New Dictionary(Of Integer, cBaseShape)
    Friend DictKeyboard As New Dictionary(Of Integer, String)
    Friend DictMovements As New Dictionary(Of Integer, cDeltas)
    Friend DictSketchScales As New Dictionary(Of Integer, CSketch)
    Friend DictTextFileFilters As New Dictionary(Of Integer, String)
    Friend DictTextFileFBackup As New Dictionary(Of Integer, String)
    Friend DictTextFileLayers As New Dictionary(Of Integer, String)
    Friend DictTextFileScript As New Dictionary(Of Integer, String)
    Friend DictOperations As New Dictionary(Of Integer, COperation)
    Friend DictRownum2Depnum As New Dictionary(Of Integer, Integer)
    Friend DictScript As New Dictionary(Of Integer, String)
    Friend DictScriptBackupCopy As New Dictionary(Of Integer, String)
    Friend DictScriptToCompare As New Dictionary(Of Integer, String)
    Friend DictSelected As New Dictionary(Of Integer, CTracckkerSelection)
    'Friend DictSettings As New Dictionary(Of Integer, String)
    '********************************
    Friend intAutoShapeTypeBeingMoved%
    Friend intBoundaryNodeCount%
    Friend intBoundaryNodePntNumList%()
    Friend intCallingForm%
    Friend intCellAddressCount%
    Friend intCellRangeDimensions%()
    Friend intCellModeDepFoundCount%
    Friend intCellModeSelCount%
    Friend intCompDepMemberCount%
    Friend intCompoundColor%
    Friend intCSVNumber%
    Friend intCSVNumberLast%
    Friend intCurrentDepictionType%
    Friend intDebugColor%
    Friend intDebugMode%
    Friend intDelete1Insert2%
    Friend intDepsInLayers%()
    Friend intDupeFinds%
    Friend intEntries%
    Friend intErrorNumber%
    Friend intFilterBitHide%
    Friend intFormNumber%
    Friend intFreeFormPointCount%
    Friend intGridlineCount%
    Friend intHelpTopic%
    Friend intHideCommandEnabled%
    Friend intLatestShapeMoved% 'Presumably moved, may simply have been selected
    Friend intLatestShapeSelected%
    Friend intLayerMoves%
    Friend intLineSlant%
    Friend intLineColorRGBSelected%
    Friend sglLineWidthSelected%
    Friend intMidpointYes1No2%
    Friend intNBN%
    Friend intOffsetDepName%
    Friend intOffsetMP%
    Friend intOutputParameterCount%
    Friend intPairsItems%
    Friend intPieSliceArcEndsAt%
    Friend intPieSliceArcPosition%
    Friend intPieSliceArcStartsAt%
    Friend intPieSliceOneLineEndsAt%
    Friend intPieSliceOneLineStartsAt%
    Friend intPieSliceOtherLineEndsAt%
    Friend intPieSliceOtherLineStartsAt%
    Friend intPointInstanceLength%
    Friend intPointListCount%
    Friend intPresentCase%
    Friend intRbnSubParam%
    Friend intRibbonIconNumber%
    Friend intIconNumberStart%
    Friend intSavedShapeCount%
    Friend intScriptRedoRow%
    Friend intScriptSpecialRow%
    Friend intScrollRow%
    Friend intSelPropertyLineCount%
    Friend intSeqNumFound%
    Friend intShapeCountSaved%
    Friend intShowDepNum%
    Friend intShownDtype%
    Friend intShownPosition%
    Friend intSketchShapeCount%
    Friend intTesting%
    Friend intTrace%
    Friend intWarningNumber%
    'Friend PairOfPoints() As MyPairOfPoints
    Friend SelCursorInfo As myCursorActions
    Friend sglAverageMeshSideLength!
    Friend sglClickedCellHeightPnts!
    Friend sglClickedCellLeftPnts!
    Friend sglClickedCellTopPnts!
    Friend sglClickedCellWidthPnts!
    Friend sglClickX1!
    Friend sglClickX2!
    Friend sglClickY1!
    Friend sglClickY2!
    Friend sglCrosshairSize!
    Friend sglDimBoxPntsX!
    Friend sglDimBoxPntsY!
    Friend sglFriendBoxPntsX!
    Friend sglFriendBoxPntsY!
    Friend sglFreeFormDataPnts!(,)
    Friend sglFreeFormOffsetHorzPnts!
    Friend sglFreeFormOffsetVertPnts!
    Friend sglFreeFormScaleHorzPPI!
    Friend sglFreeFormScaleVertPPI!
    Friend sglFudgeFactor!
    Friend sglGridSpacingUU!
    Friend sglGridUU!
    Friend sglHeightOfSelectedShapeBoxPnts!
    Friend sglLeftOfSelectedShapeBoxPnts!
    Friend sglMinimumAspectRatio!
    Friend sglMovedDxPnts!
    Friend sglMovedDyPnts!
    Friend sglOriginalDepictionPntsX1!
    Friend sglOriginalDepictionPntsParm1!
    Friend sglOriginalDepictionPntsY1!
    Friend sglOriginalDepictionPntsParm2!
    Friend sglPPI!
    Friend sglSectorAngleDeg!
    Friend sglTopOfSelectedShapeBoxPnts!
    Friend sglTSCalc!
    Friend sglTSDraw!
    Friend sglUndoValues!()
    Friend sglWidthOfSelectedShapeBoxPnts!
    Friend sglXMovedLeftPnts!
    Friend sglXMovedRightPnts!
    Friend sglXofSelectedPoint!
    Friend sglYMovedBotPnts!
    Friend sglYMovedTopPnts!
    Friend sglYofSelectedPoint!
    Friend sglZoom!
    Friend str123CADLicenseCode



这篇关于将字典写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:35