I have a winform that can stop and start IIS website remotely however I'm looking for a way to stop/start website on muliple servers by using checkedlistbox. Here's my Stop IIS website code
private void buttonStop_Click(object sender, EventArgs e)
{
string serverName = textServer.Text;
string siteName = cmbWebsite.SelectedItem.ToString();
using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote(serverName))
{
Microsoft.Web.Administration.Site site = sm.Sites.Where(q => q.Name.Equals(siteName)).FirstOrDefault();
// If the site does not exist, throw an exception
if (site == null)
{
throw new Exception("The specified site was not found!");
}
// Stop the site
site.Stop();
showStatus(siteName);
}
}
I wanted to be able to run this on multiple servers so instead of putting the server (textServer) on the text box. User can just check on which server/server they wanted to stop.
Any assistance is greatly apprecieated.
Thanks!
using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote(serverName))
{
foreach(var server in checkedListBox1.CheckedItems)
{
try
{
Microsoft.Web.Administration.Site site = sm.Sites.Where(q => q.Name.Equals(server.ToString())).FirstOrDefault();
site.Stop();
showStatus(siteName);
}
catch(Exception e) { /*handle*/ }
}
}
you just want to loop through the items
Related
There is nothing wrong in the syntax of my code but whenever I try to run it keeps saying "The process cannot access the file because it is being used by another process". The only way I am running my application is my ending my application from the task manager. Please help me by explaining why this is happening and how to fix it.
private void btnLogin_Click(object sender, EventArgs e)
{
if (File.Exists("users.txt"))
{
string[] users = File.ReadAllLines("users.txt");
bool userFound = false;
foreach (string user in users)
{
string[] splitDetails = user.Split('~');
string username = splitDetails[1];
string password = splitDetails[2];
if ((txtBoxUsername.Text == username) && (txtBoxPassword.Text == password))
{
userFound = true;
break;
}
}
if (userFound)
{
Hide();
HomeForm home = new HomeForm();
home.Show();
}
else
{
MessageBox.Show("User details are incorrect",
"Incorrect details entered");
}
}
else
{
MessageBox.Show("No users have been registered", "No users");
}
}
private void btnRegister_Click(object sender, EventArgs e)
{
Hide();
RegisterForm registerForm = new RegisterForm();
registerForm.Show();
}
This application is for my a level software systems development coursework and I am coding it in c#. I have only been learning c# for the past 5 months so I am still a beginner. I have already tried to find the answer to my problem in stack overflow and other websites.
I am expecting my application to launch when I press run, but instead I get a dialog box saying:
Error Unable to copy file "obj\Debug\SSD AS2 coursework.exe" to "bin\Debug\SSD AS2 coursework.exe". The process cannot access the file 'bin\Debug\SSD AS2 coursework.exe' because it is being used by another process.
SSD AS2 coursework
Check if you are closing all windows of your application when finalizing the app.
You must use Application.Exit() in any events that are going to finalize your application.
You can read more on the Documentation
It seems like the file you are trying to open is being used by another process try to close your text editor or another program writing to that file.
it is still possible to overcome the issue by using FileShare.ReadWrite and use the file from multiple processes, example on the following code:
FileStream fileStream = new FileStream("c:\users.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
StreamReader fileReader = new StreamReader(fileStream);
while (!fileReader.EndOfStream)
{
string user = fileReader.ReadLine();
string[] splitDetails = user.Split('~');
// the rest of the user logic in here...
}
fileReader.Close();
fileStream.Close();
I've written a custom action for an installer project that does the following:
Checks existing websites to see if any exist with the same name put
in by the user.
Creates the website in IIS if it doesn't exist.
Creates an application pool.
Assigns the application pool to the created website.
When it comes to assigning the application pool I get and error:
The configuration object is read only, because it has been committed
by a call to ServerManager.CommitChanges(). If write access is
required, use ServerManager to get a new reference.
This baffles me as it seems to suggest that I can't assign the newly created application pool with the ServerManager.CommitChanges() call. However, everything else works fine using this, which I wouldn't expect if this was an issue.
Here is my code:
I have a ServerManager instance created like so:
private ServerManager mgr = new ServerManager();
In my Install method I do the following:
Site site = CreateWebsite();
if (site != null)
{
CreateApplicationPool();
AssignAppPool(site);
}
Check existing websites - done in OnBeforeInstall method
private Site CheckWebsites()
{
SiteCollection sites = null;
Site site = null;
try
{
sites = mgr.Sites;
foreach (Site s in sites)
{
if (!string.IsNullOrEmpty(s.Name))
{
if (string.Compare(s.Name, targetSite, true) == 0) site = s;
}
}
}
catch{}
return site;
}
CreateWebSite method:
private Site CreateWebsite()
{
Site site = CheckWebsites();
if (site == null)
{
SiteCollection sites = mgr.Sites;
int port;
Int32.TryParse(targetPort, out port);
site = sites.Add(targetSite, targetDirectory, port);
mgr.CommitChanges();
}
else
{
//TO DO - if website already exists edit settings
}
return site;
}
Create App Pool
//non-relevant code...
ApplicationPool NewPool = mgr.ApplicationPools.Add(ApplicationPool);
NewPool.AutoStart = true;
NewPool.ManagedRuntimeVersion = "4.0";
NewPool.ManagedPipelineMode = ManagedPipelineMode.Classic;
mgr.CommitChanges();
Assign App Pool
private void AssignAppPool(Site site)
{
site.ApplicationDefaults.ApplicationPoolName = ApplicationPool; //ERRORS HERE
mgr.CommitChanges();
}
I can't see why a site could be created, an app pool created but then not assigned. Help.
I finally realised that the 'configuration object' referred to in the error was the 'site'. Seems obvious now, but basically I needed to re-get the site to then assign the app pool to it. I think this is allow the previous changes to take place and then pick them up. So I altered my code by removing the need to pass the Site into private void AssignAppPool() and just getting the site again like this:
Site site = mgr.Sites["TestWebApp"];
Ive added BugSense to my Windows Phone app and modified the app.xaml.cs accordingly. However, I know some users are experiencing crashes but BugSense is not seeing it. BugSense to see new sessions and what not so i know the license is correct.
I believe the crashing occurs within this code, particularly with webclient I think. What do can I add to this code so that if something occurs, BugSense will report it?
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
// verifying our sender is actually a LongListSelector
if (selector == null)
return;
SoundData data = selector.SelectedItem as SoundData;
// verifying our sender is actually SoundData
if (data == null)
return;
if (data.IsDownloaded)
{
this.PlaySound(IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open, FileAccess.Read, FileShare.Read));
}
else
{
if (!SimpleIoc.Default.GetInstance<INetworkService>().IsConnectionAvailable)
{
MessageBox.Show("You need an internet connection to download this sound.");
}
else
{
WebClient client = new WebClient();
client.DownloadProgressChanged += (senderClient, args) =>
{
Dispatcher.BeginInvoke(() =>
{
data.DownloadProgress = args.ProgressPercentage;
});
};
client.OpenReadCompleted += (senderClient, args) =>
{
using (IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(data.SavePath))
{
args.Result.Seek(0, SeekOrigin.Begin);
args.Result.CopyTo(fileStream);
this.PlaySound(fileStream);
data.Status = DownloadStatus.Downloaded;
}
args.Result.Close();
};
client.OpenReadAsync(new Uri(data.FilePath));
data.Status = DownloadStatus.Downloading;
}
}
selector.SelectedItem = null;
}
I've just started using BugSense myself in my WP8 app and I've very impressed how it catches unhandled exceptions without anything more than the single line of code in my App.xaml.cs file:
public App()
{
BugSenseHandler.Instance.InitAndStartSession(new ExceptionManager(Current), RootFrame, "YourBugSenseApiKey");
...
}
So from my experience BugSense should be catching these exceptions for you without any extra code on your part.
How do you know about these crashes? Is it from the Windows Phone Dev Center? If so then you might still be seeing crashes reported from users that have an older version of your app installed before you added BugSense.
I find that checking for a new app version from within the app itself on start-up and alerting the user is a great was to keep people up-to-date. Many users might not visit the Windows Phone Store for extended periods of time and even then may not bother to update your app to the latest version so there's a good chance you have a lot of users on old versions.
I am creating a web app. where I want be able to incorporate Google Maps into 1 of my pages.
From what I have read in other places, the easiest think is to place a web browser onto the form but there is no 'Web Browser' in the tool-box.
What I am trying to do is to insert a location into a textbox(ie. London) and insert a type of sport(ieCycling) and the resultant map shows up. Is there any other way in doing this in C# other than using the web browser tool.
Here is my code:
protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error");
}
} protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
I tried to put the address into a panel but this is clearly wrong. Any help would be greatly appreciated!
It seems like you are very confused and mixing ASP.NET Web Forms with Windows Forms.
Specifically, MessageBox.Show() would open a Windows message box, not a browser window. And it would happen on the server side for whatever user your web server runs as. Probably not the desired intention. Also, you can't "put a web browser" onto a page. There is a WebBrowser control for Windows Forms, which embeds a minified version of Internet Explorer into a Windows application. But again, probably not what you want.
ASP.NET can be used as if it were just a normal HTML site. So find some Google Maps tutorials for HTML and follow those.
I am deploying a C# application on client machine. The Application need to access code from another program, so it can scrap text from the screen of another application. It is running fine on the development machine but on the client machine it is throwing an error "ActiveX Component cannot create Object" this is where i am getting the error from!
private ExtraSession objExtraSession;
private ExtraSessions objExtraSessions;
private ExtraScreen objExtraScreen;
private ExtraArea objExtraArea;
private ExtraSystem objExtraSystem;
protected void sessionInitializer()
{
try
{
objExtraSystem = (ExtraSystem) Microsoft.VisualBasic.Interaction.CreateObject("Extra.system");
if (objExtraSystem == null)
{
MessageBox.Show("Could not create system");
return;
}
objExtraSessions = objExtraSystem.Sessions;
if (objExtraSessions == null)
{
MessageBox.Show("Could not create sessions");
return;
}
if (!System.IO.File.Exists("C:\\Users\\" + userid + "\\Documents\\Attachmate\\EXTRA!\\Sessions\\SAS.edp"))
{
MessageBox.Show("File does not exist");
return;
}
objExtraSession = (ExtraSession) Microsoft.VisualBasic.Interaction.GetObject("C:\\Users\\"+ userid + "\\Documents\\Attachmate\\EXTRA!\\Sessions\\SAS.edp");
if (objExtraSession == null)
{
MessageBox.Show("Could not create session");
return;
}
if (objExtraSession.Visible == 0)
{
objExtraSession.Visible = 1;
}
objExtraScreen = objExtraSession.Screen;
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, "Failed to initialize Attachmate sessions");
}
}
The error is generated from objExtraSession = (ExtraSession) Microsoft.VisualBasic.Interaction.GetObject("C:\Users\"+ userid + "\Documents\Attachmate\EXTRA!\Sessions\SAS.edp");
Am I missing some step. Please help me out. Thanks in advance.
The most likely explanation is that your development machine has the ActiveX control installed, but the client machine does not. Read the deployment documentation for the control and do what is says is required to deploy to the client machine.
Thanks for all your responses... The method GetObject was creating an object whose activex component was not registered... I resolved by finding the corresponding *.ocx file and calling Regsvr32 on the file this resolved the problem...