在我的 c# 程序(使用 Visual Studio 2010)中,我正在将文件上传到 SharePoint 文档库,问题是我正在运行我们的内存,显然进程“sqlservr.exe * 32”就像超过 1 GB 的内存。要上传文件,我首先将文件的字节读入 byte[] 数组。然后将该数组作为文件上传到文档库。我需要一种在上传每个文件之前清除内存的方法。
该程序在循环中上传文件,该循环在目录中迭代。那么有没有办法清除 byte[] 数组的内存呢?
实际上有没有办法可以重新启动进程“sqlservr.exe * 32”来释放内存?
这是我用来上传的代码:
我在网站上看到的错误消息是(当问题发生时,我认为是内存不足,有时消息中的文件也不同): URL 'Test Library/myfolder/file.txt' 无效. 它可能引用不存在的文件或文件夹,或者引用不在当前 Web 中的有效文件或文件夹。
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
using Microsoft.SharePoint;
namespace CustomApplicationPage.Layouts.CustomApplicationPage
{
public partial class CustomApplicationPage : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
// Check if user posted a file
if (File1.PostedFile == null)
{
return;
}
try
{
// Get the directories in the path
string[] array1 = Directory.GetDirectories("C:\\myfolder\\");
// Declare variables
string[] temp;
byte[] contents;
string dir_name;
string file_name;
int i, j;
int chunk_size = 5;
int chunk_index = 0;
int start_id = chunk_size * chunk_index;
int end_id = 1 + (chunk_size * (chunk_index+1));
// Free the memory
GC.Collect();
// For each directory in the main path
for (i = 0; i < array1.Length; i++)
{
// Get directory name
dir_name = Path.GetFileName(array1[i]);
// Skip this file
if (dir_name == "viDesktop_files" || i < start_id || i >= end_id)
{
continue;
}
// Get the site
SPSite site = new Microsoft.SharePoint.SPSite("http://mysite/");
// Turn security off
Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
// Get site root
SPWeb spWeb = site.RootWeb;
// Get the specified list/library from the site root
SPList docLib = spWeb.Lists["Test Library"];
// Get all files in the directory
temp = Directory.GetFiles(array1[i]);
// Create a folder in the list/library
SPListItem folder = docLib.Folders.Add(docLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dir_name);
// Activate the newly created folder
folder.Update();
// For each file in the directory
for (j = 0; j < temp.Length; j++)
{
// Get file name
file_name = Path.GetFileName(temp[j]);
// If the file is a .mht file
if (file_name.EndsWith(".mht"))
{
// Read the contents of the uploaded file into the variable 'contents'
contents = File.ReadAllBytes(temp[j]);
// Add the file with the specified filename in the folder
SPFile file = folder.Folder.Files.Add(file_name, contents);
// Activate the file
file.Update();
}
}
// Turn security on
webApp.FormDigestSettings.Enabled = true;
// Close the site
site.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}