File.ReadAllText and after File.WriteAllText - c#

My questions is, when i run this code its tell me on "File.WriteAllText" line procedure.pas is using by another program.
script = File.ReadAllText(Application.StartupPath + "\\Procedure.pas");
decryptedstring = Crypter.Decrypt(script, sSecretKey);
decryptedstring = decryptedstring.Replace("_" + Firma + "_", "_" + txtFirma.Text + "_");
decryptedstring = decryptedstring.Replace("_" + DonemNo + "_", "_" + txtDonemNo.Text + "_");
decryptedstring = decryptedstring.Replace("FIRMNR=" + Firma + " ", "FIRMNR=" + txtFirma.Text + " ");
encryptedstring = Crypter.Encrypt(decryptedstring, sSecretKey);
File.WriteAllText(Application.StartupPath + "\\Procedure.pas", encryptedstring);

It is possible that the file you are writing is opened with a shared lock that allows reading data by other processes but does not allow writing. So when you are opening the file for write, it fails with the exception although the file was opened by another process the whole time.
See FileShare enumeration for details on this.

Related

Cannot send string to remove licenses for user with the Set-MsolUserLicense command

I am trying to use Set-MsolUserLicense Msol Powershell command in c# to remove licenses from a user with the string
string removeLicense = "Set-MsolUserLicense -UserPrincipalName " + "\""
+ selectedUPN + "\"" + " -RemoveLicenses " + accountSkuId + ":ENTERPRISEPACK";
where selectedUPN is the selected option obtained from a textbox.
Somehow it does not accept a string but if I type the UPN literally in the removelicense string it works.
Finally Managed to get this sorted...created string as follows and it worked:
string removeLicense = "Set-MsolUserLicense -UserPrincipalName \"" + selectedUPN + "\" -RemoveLicenses " + accountSkuId + ":ENTERPRISEPACK";

Value of a variable is not updating

I am currently working on a project that sweeps a mailbox for attachments and when one is found it is placed in the user's directory. My problem is that when I check if the file exist in the path, I alter the attachment's name and add a counter and time stamp, that way it is not over written. However, when it goes into the condition and changes the file name it never updates the path variable to include the right value of the Clean name variable.
string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);
string path = employeeStarPath + "\\" + cleanName;
// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{
cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.
}
Now I am aware I can update the path var by calling path = employeeStarPath + "\\" + cleanName; again but I feel that this makes my code a bit confusing.
I might not understood your question but can you just call the line "string path = employeeStarPath + "\" + cleanName;" at the end instead before the if?
string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);
// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{
cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.
}
string path = employeeStarPath + "\\" + cleanName;

Internal Error !scandr.cpp #1952 in autocad2015

I am trying to export the drawing to save another drawing using "CopyBase" and "PasteClip" command. But it is does not work an error occurred. Can anyone tell how to i solve this..
i am using Copy and Paste command like this..
AcadApplication acadApp;
AcadDocument thisdrawing;
acadApp = Marshal.GetActiveObject("AutoCAD.Application.20") as AcadApplication;
acadApp.Visible = true;
thisdrawing= acadApp.ActiveDocument;
thisdrawing.Activate();
string str = "_CopyBase" + char.ConvertFromUtf32(13) + "0,0,0" + char.ConvertFromUtf32(13) + "M" + char.ConvertFromUtf32(13) + "G" + char.ConvertFromUtf32(13) + "QWERT" + "\n" + char.ConvertFromUtf32(13);
thisdrawing.SendCommand(str);
string dwgTempPath = "acad.dwt";
newThisdrawing = acapp.Documents.Add(dwgTempPath ) ;
newThisdrawing.SaveAs(expDwgName , thisdrawing.Application.Preferences.OpenSave.SaveAsType,null);
newDwgCreatBool = true;
newThisdrawing.Regen(AcRegenType.acActiveViewport);
newthisdrawing.Activate();
comStr = "pasteclip" + "\n" + "0,0" + "\n";
newThisdrawing.SendCommand(comStr);
Thanks in Advance..
With your edit, I'm assuming this is an external exe. With that in mind and some minor modifications to your code, I got this to work with no problems in a Console exe. Before the code a couple things about it:
I compiled using the 4.5 Framework. The COM references for .NET may have been updated with their .NET API counterparts, so make sure this is the case for you too.
Add a COM reference to "AutoCAD 2015 Type Library" there's one for each language package (choose axdb20enu.tlb, but I think in the end VS may select them all after it's added).
Add a COM reference to "AcObjClassImp 1.0 Type Library", choose the one for release 20 (AcObjClassImp20.tlb)
Add the using statement "using AutoCAD;" at the top with the rest.
Here's the code I used:
static void Main(string[] args)
{
IAcadApplication acadApp;
IAcadDocument thisdrawing;
acadApp = Marshal.GetActiveObject("AutoCAD.Application.20") as AcadApplication;
acadApp.Visible = true;
thisdrawing = acadApp.ActiveDocument;
thisdrawing.Activate();
string str = "_CopyBase" + char.ConvertFromUtf32(13) + "0,0,0" + char.ConvertFromUtf32(13) + "M" + char.ConvertFromUtf32(13) + "G" + char.ConvertFromUtf32(13) + "QWERT" + "\n" + char.ConvertFromUtf32(13);
thisdrawing.SendCommand(str);
string dwgTempPath = "acad.dwt";
IAcadDocument newThisdrawing = acadApp.Documents.Add(dwgTempPath);
//Instead of the path below, use your full path, formatted correctly
newThisdrawing.SaveAs(#"C:\Acad\Test.dwg", thisdrawing.Application.Preferences.OpenSave.SaveAsType, null);
newThisdrawing.Regen(AcRegenType.acActiveViewport);
newThisdrawing.Activate();
string comStr = "pasteclip" + "\n" + "0,0" + "\n";
newThisdrawing.SendCommand(comStr);
}

Output from an Exception class in App_Code

I am trying to see what exception is happening on my dev clients devserver (our servers are fine) and I have an TestException.cs class that handles the most of my exceptions from my sql statement class.
I have a method like so:
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < innerException.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + innerException.Errors[i].Message + "\n" +
"LineNumber: " + innerException.Errors[i].LineNumber + "\n" +
"Source: " + innerException.Errors[i].Source + "\n" +
"Procedure: " + innerException.Errors[i].Procedure + "\n");
}
//Console.WriteLine(errorMessages.ToString());
Response.Clear();
Response.Write("FAILURE");
Response.End();
But the Response.XX won't compile and if I use console.writeline when it hits it on the client server, the browser gets a connection lost webpage.
Is there a better way to do this?
You could try using the Trace class. If you link with a tracewriter in web.config pointing to a text file you can generate logging with Trace.WriteLine. Then you would just need the file uploaded to your network for reviewing.

How can I release/dispose a string file for reading after used File.Create?

I did:
if (averagesListTextFile != null)
{
Directory.CreateDirectory(subDirectoryName);
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt");
And then I want to do:
reader = new StreamReader(subDirectoryName + "\\" + averagesListTextFile + ".txt");
But I'm getting error say the file is in use by another process...And that happen only after I did the File.Create
File.Create returns a stream, so you need to dispose it:
using (File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt"))
{
}
or equivalently in this case:
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt").Dispose();
But if you've just created the file, why would you try to read it? It will be empty.
Note that your reader should use a using statement too. Alternatively, to read and write complete text files, you should look into File.WriteAllText and File.ReadAllText, which make life simpler.

Categories