Sto usando C# framework 4.5, netoffice 1.6 e sharpdevelop 4.4.1 per manipolare una cartella di lavoro Excel, che si trova su una condivisione di rete, da Outlook.Ottiene automaticamente l'utente che sta bloccando una cartella di lavoro excel
Ad un certo punto ho bisogno di cambiare l'accesso ai file dell'oggetto cartella di lavoro (EWB) per readwrite in questo modo:
ewb.ChangeFileAccess(Excel.Enums.XlFileAccess.xlReadWrite, System.Reflection.Missing.Value, true);
Prima di cambiare l'accesso del file, controllo se il file è bloccato sul server . Se il file è bloccato, notificherò all'utente di riprovare l'azione in un secondo momento.
Ora, voglio includere il nome utente che blocca il file excel nella notifica. Ho cercato msdn, forum di netoffice, eccetera ... e non ho trovato una soluzione. So che, se apri il file excel readwrite, esso memorizzerà il nome dell'utente nel file xlsx. Come posso accedere a quella particolare informazione attraverso C#?
EDIT: ho finito per fare questo:
public string GetExcelFileOwner(string path, NetOffice.ExcelApi.Enums.XlFileFormat ffmt) {
string tempmark = "~$";
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
tempmark = "";
}
string uspath = Path.Combine(Path.GetDirectoryName(path), tempmark + Path.GetFileName(path));
if (!File.Exists(uspath)) return "";
var sharing = FileShare.ReadWrite | FileShare.Delete;
using (var fs = new FileStream(uspath, FileMode.Open, FileAccess.Read, sharing))
using (var br = new BinaryReader(fs, Encoding.Default)) {
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
byte[] ByteBuffer = new byte[500];
br.BaseStream.Seek(150, SeekOrigin.Begin);
br.Read(ByteBuffer, 0, 500);
return matchRegex(System.Text.Encoding.UTF8.GetString(ByteBuffer), @"(?=\w\w\w)([\w, ]+)").Trim();
}
else {
return br.ReadString();
}
}
}
private static string matchRegex(string txt, string rgx) {
Regex r;
Match m;
try {
r = new Regex(rgx, RegexOptions.IgnoreCase);
m = r.Match(txt);
if (m.Success) {
return m.Groups[1].Value.ToString();
}
else {
return "";
}
}
catch {
return "";
}
}
Stiamo usando Excel 2003 ed Excel 2007+ formato di file (.xls e .xlsx). Per .xls ho dovuto cercare nel file .xls stesso. Per .xlsx, l'utente che blocca è memorizzato nel file ~ $ temp. Lo so, per il file .xls, è un codice sporco, ma non ho idea di come sia strutturato il formato file .xls. Pertanto, ho appena letto un sacco di byte che include il nome utente ascii e faccio una regex per estrarre quel nome utente.
hai provato a guardare Win32_ConnectionShare e WMIC? –