|
I do not know if this will work but I know when I use string you have to be careful of slashes and so on so for one you could try:
private void LoadSettings()
{
StreamReader Settings = new StreamReader("Settings.txt");
string line;
while ((line = Settings.ReadLine()) != null)
{
if (line.StartsWith(@"/"))
{
}
else
{
string[] splitArray = line.Split(new char[] { '=' });
MessageBox.Show(splitArray[0]);
}
}
}
The at symbol will ensure it is read as string....
as for getting the strings back together you could use a string builder or just add the values together
string var = (splitArray[0] + " = " + splitArray[1]);
I am not sure if that is the best way to do it. Hope that helps.
|
|
|
|
|
Hum im getting System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
i think it might have something to do with splitArray[0] being empty , The line brakes seem to have gotten through
|
|
|
|
|
The MSDN on 'is' operator says ...the provided object can be cast to the provided type without causing an exception to be thrown. Suppose I have the following code:
object x = FunctionThatReturnsAnInteger();
if(x is decimal)
Console.WriteLine("Hello world");
The above condition is never true because x is of type integer, not decimal, but - int can be cast to decimal without an exception being thrown - so why does it not work?
More directly to my problem - what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal? I can obviously try this in a try-catch block, but I wonder if there are ways that perform better.
Thanks for sugegstions, H.
|
|
|
|
|
These are integral types, is only works for classes. This should not compile. The is operator WILL work with a class structure.
class Base
class Derived1 : Base
Derived1 n;
if ( n is Base )
{
// this will work
}
Member 1033907 wrote: what is the fastest (in runtime) way to check, if a variable (of type 'object', in general) can be cast to decimal?
I suspect you need your try/catch block. You also have the cost of boxing to contend with. You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Thanks for your reply.
Christian Graus wrote: This should not compile.
I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.
Christian Graus wrote: You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
|
|
|
|
|
Member 1033907 wrote: I compiles fine, 'is' works with structures as well.
I didn't think that was the case. I know I've had 'is' not compile for me before because I used an integral type.
Member 1033907 wrote: String parsing seems to me like the slowest possible approach
Well, you're already paying the cost of boxing, which is expensive, and talking about try/catch, which is VERY expensive.
Member 1033907 wrote: I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
I would do some testing to see which is faster.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
I've got it. It's 'as' that doesn't work, because int cannot be null. But, is and as both rely on an inheritance tree, they do not try to do any sort of conversion between types. decimal does not derive from int, or vice versa, so neither of those will work. You're asking if the type can be converted, not if it is of that type to start with.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Member 1033907 wrote: int can be cast to decimal without an exception being thrown - so why does it not work?
Just because you can cast to it does not mean it is part of the same type heirarchy. Types can provide their own casting operators to make coercion to other types easier for the programmer.
is /as only work on things when they are along the same branch of an object heirarchy.
decimal derives from object . int derives from object . Each is heading down different branches.
Man who stand on hill with mouth open wait long time for roast duck to drop in
|
|
|
|
|
The term "cast" is used to mean two different things, one of which is more like "convert".
When an int is "cast" to decimal it gets converted. The is operator doesn't work with that type of cast.
|
|
|
|
|
I wrote an app which creates excel reports. I would like to kill all the excel processes my application started. How do I figure out if my application started the process so I only kill those processes?
I have this code so far:
foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
p.Kill();
}
However, as it is clear from the code, it kills all excel processes.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Hi,
AFAIK there ever is only one Excel process; opening an Excel document causes Excel to start if it isn't already running; opening a second Excel document gets directed towards the first Excel process. So you can only kill one Excel process, and doing so may be a mistake since it may have open documents your app did not open. Your app should tell Excel to close documents, and optionally, when no more documents are open, it could tell Excel to exit. Under no circumstances it should kill Excel.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: AFAIK there ever is only one Excel process
You can use the .IgnoreRemoteRequests = true property to force excel to open in multiple instances for each workbook, so sometimes you would need a loop.
My question is if he opens excel in his code to generate reports, there is no need to terminate them abnormally, especially when a client may have unrelated excel instances open unrelated to his code. He can simply terminate them through his code by using .quit, can't he?
|
|
|
|
|
No, there are actually separate processes created. Excel 2003
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Why don't you just quit the excel instance through your code?
|
|
|
|
|
I tried Quit() along with setting it to null but the process still existed. Any other idea?
I am thinking of logging the time my app starts and then checking the start time of each process. If the start time is bigger than start time of app and created by the same user then I will kill it. But it is still possible, the user opened an excel file outside my app while my app is active so this is not the best solution.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Did you try something along the lines of
Where _excelApplication = Excel.Application
_excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication);
?
|
|
|
|
|
Just did both quit and ReleaseComObject methods but still there.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Without seeing your code I'd say your out of luck.
|
|
|
|
|
Hope you can follow it through since I have written it in VB.NET.
This is just a wrapper. The parts which might be of interest to you are the constructor and the Create method.
Public Class ExcelBook
Implements IObservable
Private _parameters As Parameters
Private _repoHelper As RepositoryHelper
Private _sheets As Dictionary(Of String, Worksheet)
Private _workBook As Workbook
Private _xlApp As Application
Private _summarySheet As ExcelSheet
Public Sub New(ByVal params As Parameters)
Me._xlApp = New Application()
Me._workBook = Me._xlApp.Workbooks.Add()
Me._repoHelper = Nothing
Me._sheets = New Dictionary(Of String, Worksheet)(53)
Me._parameters = params
End Sub
Private Sub DeleteDefaultSheets()
Dim DeadSheet As Worksheet
DeadSheet = Me._workBook.Sheets("Sheet1")
DeadSheet.Delete()
DeadSheet = Me._workBook.Sheets("Sheet2")
DeadSheet.Delete()
DeadSheet = Me._workBook.Sheets("Sheet3")
DeadSheet.Delete()
End Sub
Public Sub Create()
Me.CreateRepositories()
Dim xSheet As ExcelSheet = Nothing
Dim sheetSpecs As Sheet = Nothing
Dim wSheet As Worksheet = Nothing
' If the report has a detail sheet and range is multiple for month or week or season then provide detail sheet specs to
' ExcelSheet constructor to create sheets with specification of the detail sheet
' First sheet must be reserved for the summary sheet
' After all detail sheets are finished, a summary sheet must be created.
Dim stepInfo As New StepEventArgs()
If (Me._parameters.Report.HasDetailSheet And Me._parameters.IsMultipleRanges) Then
' Details Sheets
sheetSpecs = Me._parameters.Report.DetailSheet
For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
wSheet.Name = repo.Key
xSheet = New ExcelSheet(wSheet, sheetSpecs, repo.Value, False, Me._parameters, Me._workBook)
xSheet.Fill()
Me._sheets.Add(repo.Key, xSheet.SpreadSheet)
stepInfo.Message = repo.Key & " completed."
RaiseEvent StepCompleted(Me, stepInfo)
Next
Me.DeleteDefaultSheets()
' All detail sheets are done now do the summary
Dim repositoryTemplate As Repository = Nothing
For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper
repositoryTemplate = repo.Value
Exit For
Next
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
wSheet.Name = "Summary"
' Summary Sheet
sheetSpecs = Me._parameters.Report.SummarySheet
xSheet = New ExcelSheet(wSheet, sheetSpecs, repositoryTemplate, True, Me._parameters, Me._workBook)
Me._sheets.Add("Summary", xSheet.SpreadSheet)
Me._summarySheet = xSheet
Me._summarySheet.Fill()
stepInfo.Message = "Summary sheet completed."
RaiseEvent StepCompleted(Me, stepInfo)
Else ' Report has only one sheet
sheetSpecs = Me._parameters.Report.SummarySheet
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
Me.DeleteDefaultSheets()
wSheet.Name = "Summary"
xSheet = New ExcelSheet(wSheet, sheetSpecs, Me._repoHelper.Repository, False, Me._parameters, Me._workBook)
xSheet.Fill()
Me._sheets.Add("Summary", xSheet.SpreadSheet)
stepInfo.Message = "Summary sheet completed."
RaiseEvent StepCompleted(Me, stepInfo)
End If
Dim path As String = My.Settings.FilePath & "Report - " & Now.Hour.ToString() & "-" & Now.Minute.ToString() & Me._parameters.Div.ToString() & ".xls"
'path = Me._workBook.Name
Me.FreezeColumns(Me._sheets("Summary").Range("C8:C8"))
Me._workBook.SaveAs(path, Excel.XlFileFormat.xlExcel9795)
Me._xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(Me._xlApp)
'Me._xlApp.DisplayAlerts = True
'Me._xlApp.ScreenUpdating = True
'Me._xlApp.Visible = True
End Sub
Private Sub AddToSummarySheet(ByVal spreadSheet As ExcelSheet)
' Ask the sheet to give you its formula in a dictionary for each column and row
End Sub
Private Sub CreateRepositories()
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Weekly) Then
Me._repoHelper = New RepositoryHelperWeekly()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Monthly) Then
Me._repoHelper = New RepositoryHelperMonthly()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Seasonal) Then
Me._repoHelper = New RepositoryHelperSeasonally()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
End Sub
Private Sub FreezeColumns(ByVal range As Range)
range.Select()
Me._xlApp.ActiveWindow.FreezePanes = True
End Sub
Public Event StepCompleted(ByVal sender As Object, ByVal e As StepEventArgs) Implements IObservable.StepCompleted
End Class
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
next time, post your question in the vb.net forums, it really helps to know what language you are dealing with.
Secondly, use the pre tags.
Thirdly, this is how I did it (in vb.net)
Public Sub Terminate()
If _excelApplication IsNot Nothing Then
_excelApplication.DisplayAlerts = False
_excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication)
_excelApplication = Nothing
_excelApplication.Finalize()
End If
End Sub
jeez.
|
|
|
|
|
Sorry about that. The reason I posted it here is because I did not think it will get to the point where I need to post code.
Still no luck. The process exists after doing all of the above.
EliottA wrote: Thirdly, this is how I did it (in vb.net)
You mean it worked for you? Finalize() is not a member of Excel.Application.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Sorry, did not mean to throw that line in there.
CodingYoshi wrote: he reason I posted it here is because I did not think it will get to the point where I need to post code.
Still belongs in the VB.Net forum.
CodingYoshi wrote: The process exists after doing all of the above.
Err....ummm....does the process exist before execution...?
|
|
|
|
|
No, I manually check to make sure no process exists before the execution and I also kill all the instances programmatically just to reassure using this code:
foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
p.Kill();
}
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Again that is horrible design, what if a client has excel running?
Create a new instance of excel, and set it's IgnoreRemoteRequests property to true, this will cause *your* instance of excel to operate under a new process.
|
|
|
|
|
How do you create excell processes?
System.Diagnostics.Process.Start("...") Something like that? If so save the process that you create.
List<Process> list = new List<Process>();
Process p = System.Diagnostics.Process.Start("...");
list.Add(p);
...
list[i].Kill();
|
|
|
|